{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Reporting using Pandas - Going Beyond Basics 🐼\n",
    "\n",
    "<img src=\"images/00_reporting.PNG\">\n",
    "\n",
    "\n",
    "## What's covered in this notebook?\n",
    "1. Aggregating statistics grouped by category  \n",
    "\t- Reading a .csv File - Online Store Sales Data  \n",
    "\t- Grouping the Data on the basis of Product Category  \n",
    "        - Returning all the groups and row indexes  \n",
    "        - Get unique group keys  \n",
    "        - Filter data on the basis of group keys  \n",
    "        - Returning first row, last row and nth row for each group  \n",
    "\t- Grouping the Data Based on Product Category and Sub-Category  \n",
    "\t\t- Returning all the groups and row indexes\n",
    "\t\t- Get unique group keys\n",
    "\t\t- Filter data on the basis of group keys\n",
    "\t\t- Returning first row, last row and nth row for each group\n",
    "\t- split-apply-combine  \n",
    "\t- Aggregation  \n",
    "\t\t- Built-in Aggregation Methods\n",
    "\t\t- Aggregation with User-Defined Functions\n",
    "\t\t- Applying different aggregation functions to DataFrame columns\n",
    "\t- Filteration  \n",
    "\t\t- Built-in Filteration\n",
    "\t\t- Filteration with User-Defined Functions\n",
    "\t- Transformation  \n",
    "\t\t- Built-in Transformation\n",
    "\t\t- Transformation with User-Defined Functions\n",
    "2. Solving a Case Study using groupby()  \n",
    "\t- Reading a .csv File - Online Store Sales Data  \n",
    "\t- What are the different customer segments?  \n",
    "\t- How many sales records do we have in the dataset?  \n",
    "\t- What are the different product categories?  \n",
    "\t- How many days on average it take for the products to get shipped?  \n",
    "\t- Are there more orders placed on weekends?  \n",
    "\t- What is the minimum order amount and maximum order amount?  \n",
    "\t- What is the revenue generated in the year 2017?  \n",
    "\t- Which customer contributed to the maximum revenue in 2017 and how much?  \n",
    "\t- Who is the customer with customer_id == TC-20980 ?  \n",
    "\t- Which region recorded maximum sales count?  \n",
    "\t- Which product category is doing best? (revenue and count)\n",
    "3. Analysing and Summarizing using pivot_table()\n",
    "\t- What is the region-wise revenue?\n",
    "\t- What is the region-wise count of sales?\n",
    "\t- What is the region-wise count and sum of sales?\n",
    "\t- What is the region-wise revenue generated of each product category?\n",
    "\t- What is the region-wise revenue generated of each product sub-category under product category?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Aggregating statistics grouped by category\n",
    "\n",
    "<img style=\"float: right;\" width=\"400\" height=\"400\" src=\"images/01_groupby.PNG\">\n",
    "\n",
    "**Question: How to calculate summary statistics?**  \n",
    "**Answer:** Basic statistics (mean, median, min, max, counts…) are easily calculable. These or custom aggregations can be applied on the entire data set, a sliding window of the data, or grouped by categories. The latter is also known as the split-apply-combine approach.\n",
    "\n",
    "**Important Note**  \n",
    "`groupby()` and `pivot_table()` are very powerful in analysing and summarizing the data. `pivot_table()` are more powerful when applying complex aggregation operations."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Reading a .csv File - Online Store Sales Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Row ID</th>\n",
       "      <th>Order ID</th>\n",
       "      <th>Order Date</th>\n",
       "      <th>Ship Date</th>\n",
       "      <th>Ship Mode</th>\n",
       "      <th>Customer ID</th>\n",
       "      <th>Customer Name</th>\n",
       "      <th>Segment</th>\n",
       "      <th>Country</th>\n",
       "      <th>City</th>\n",
       "      <th>State</th>\n",
       "      <th>Postal Code</th>\n",
       "      <th>Region</th>\n",
       "      <th>Product ID</th>\n",
       "      <th>Category</th>\n",
       "      <th>Sub-Category</th>\n",
       "      <th>Product Name</th>\n",
       "      <th>Sales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>CA-2017-152156</td>\n",
       "      <td>2017-11-08</td>\n",
       "      <td>2017-11-11</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>CG-12520</td>\n",
       "      <td>Claire Gute</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Henderson</td>\n",
       "      <td>Kentucky</td>\n",
       "      <td>42420.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-BO-10001798</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Bookcases</td>\n",
       "      <td>Bush Somerset Collection Bookcase</td>\n",
       "      <td>261.9600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>CA-2017-152156</td>\n",
       "      <td>2017-11-08</td>\n",
       "      <td>2017-11-11</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>CG-12520</td>\n",
       "      <td>Claire Gute</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Henderson</td>\n",
       "      <td>Kentucky</td>\n",
       "      <td>42420.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-CH-10000454</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Chairs</td>\n",
       "      <td>Hon Deluxe Fabric Upholstered Stacking Chairs,...</td>\n",
       "      <td>731.9400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>CA-2017-138688</td>\n",
       "      <td>2017-06-12</td>\n",
       "      <td>2017-06-16</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>DV-13045</td>\n",
       "      <td>Darrin Van Huff</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90036.0</td>\n",
       "      <td>West</td>\n",
       "      <td>OFF-LA-10000240</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Labels</td>\n",
       "      <td>Self-Adhesive Address Labels for Typewriters b...</td>\n",
       "      <td>14.6200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>US-2016-108966</td>\n",
       "      <td>2016-10-11</td>\n",
       "      <td>2016-10-18</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>SO-20335</td>\n",
       "      <td>Sean O'Donnell</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Fort Lauderdale</td>\n",
       "      <td>Florida</td>\n",
       "      <td>33311.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-TA-10000577</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Tables</td>\n",
       "      <td>Bretford CR4500 Series Slim Rectangular Table</td>\n",
       "      <td>957.5775</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>US-2016-108966</td>\n",
       "      <td>2016-10-11</td>\n",
       "      <td>2016-10-18</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>SO-20335</td>\n",
       "      <td>Sean O'Donnell</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Fort Lauderdale</td>\n",
       "      <td>Florida</td>\n",
       "      <td>33311.0</td>\n",
       "      <td>South</td>\n",
       "      <td>OFF-ST-10000760</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Storage</td>\n",
       "      <td>Eldon Fold 'N Roll Cart System</td>\n",
       "      <td>22.3680</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Row ID        Order ID Order Date  Ship Date       Ship Mode Customer ID  \\\n",
       "0       1  CA-2017-152156 2017-11-08 2017-11-11    Second Class    CG-12520   \n",
       "1       2  CA-2017-152156 2017-11-08 2017-11-11    Second Class    CG-12520   \n",
       "2       3  CA-2017-138688 2017-06-12 2017-06-16    Second Class    DV-13045   \n",
       "3       4  US-2016-108966 2016-10-11 2016-10-18  Standard Class    SO-20335   \n",
       "4       5  US-2016-108966 2016-10-11 2016-10-18  Standard Class    SO-20335   \n",
       "\n",
       "     Customer Name    Segment        Country             City       State  \\\n",
       "0      Claire Gute   Consumer  United States        Henderson    Kentucky   \n",
       "1      Claire Gute   Consumer  United States        Henderson    Kentucky   \n",
       "2  Darrin Van Huff  Corporate  United States      Los Angeles  California   \n",
       "3   Sean O'Donnell   Consumer  United States  Fort Lauderdale     Florida   \n",
       "4   Sean O'Donnell   Consumer  United States  Fort Lauderdale     Florida   \n",
       "\n",
       "   Postal Code Region       Product ID         Category Sub-Category  \\\n",
       "0      42420.0  South  FUR-BO-10001798        Furniture    Bookcases   \n",
       "1      42420.0  South  FUR-CH-10000454        Furniture       Chairs   \n",
       "2      90036.0   West  OFF-LA-10000240  Office Supplies       Labels   \n",
       "3      33311.0  South  FUR-TA-10000577        Furniture       Tables   \n",
       "4      33311.0  South  OFF-ST-10000760  Office Supplies      Storage   \n",
       "\n",
       "                                        Product Name     Sales  \n",
       "0                  Bush Somerset Collection Bookcase  261.9600  \n",
       "1  Hon Deluxe Fabric Upholstered Stacking Chairs,...  731.9400  \n",
       "2  Self-Adhesive Address Labels for Typewriters b...   14.6200  \n",
       "3      Bretford CR4500 Series Slim Rectangular Table  957.5775  \n",
       "4                     Eldon Fold 'N Roll Cart System   22.3680  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('data/online_store_sales.csv', parse_dates=[\"Order Date\", \"Ship Date\"], dayfirst=True)\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 9800 entries, 0 to 9799\n",
      "Data columns (total 18 columns):\n",
      " #   Column         Non-Null Count  Dtype         \n",
      "---  ------         --------------  -----         \n",
      " 0   Row ID         9800 non-null   int64         \n",
      " 1   Order ID       9800 non-null   object        \n",
      " 2   Order Date     9800 non-null   datetime64[ns]\n",
      " 3   Ship Date      9800 non-null   datetime64[ns]\n",
      " 4   Ship Mode      9800 non-null   object        \n",
      " 5   Customer ID    9800 non-null   object        \n",
      " 6   Customer Name  9800 non-null   object        \n",
      " 7   Segment        9800 non-null   object        \n",
      " 8   Country        9800 non-null   object        \n",
      " 9   City           9800 non-null   object        \n",
      " 10  State          9800 non-null   object        \n",
      " 11  Postal Code    9789 non-null   float64       \n",
      " 12  Region         9800 non-null   object        \n",
      " 13  Product ID     9800 non-null   object        \n",
      " 14  Category       9800 non-null   object        \n",
      " 15  Sub-Category   9800 non-null   object        \n",
      " 16  Product Name   9800 non-null   object        \n",
      " 17  Sales          9800 non-null   float64       \n",
      "dtypes: datetime64[ns](2), float64(2), int64(1), object(13)\n",
      "memory usage: 1.3+ MB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['row_id', 'order_id', 'order_date', 'ship_date', 'ship_mode',\n",
       "       'customer_id', 'customer_name', 'segment', 'country', 'city', 'state',\n",
       "       'postal_code', 'region', 'product_id', 'category', 'sub_category',\n",
       "       'product_name', 'sales'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "col_names = [ col.strip().lower().replace(' ', '_').replace('-', '_') for col in df.columns ]\n",
    "\n",
    "df.columns = col_names\n",
    "\n",
    "df.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Grouping the Data on the basis of Product Category"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "grouped_df = df.groupby('category')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Returning all the groups and row indexes\n",
    "\n",
    "The `groups` attribute is a dictionary whose keys are the computed unique groups and corresponding values are the axis labels belonging to each group."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Furniture': [0, 1, 3, 5, 10, 23, 24, 27, 29, 36, 38, 39, 51, 52, 57, 65, 66, 72, 73, 76, 78, 85, 93, 96, 104, 110, 117, 119, 124, 125, 128, 129, 139, 140, 146, 149, 157, 167, 173, 177, 189, 192, 201, 204, 213, 222, 226, 228, 229, 231, 232, 234, 238, 239, 241, 242, 244, 249, 254, 272, 282, 292, 293, 294, 295, 301, 303, 304, 309, 310, 311, 313, 317, 325, 328, 338, 354, 362, 364, 369, 377, 384, 387, 399, 408, 412, 413, 415, 417, 422, 424, 425, 439, 440, 444, 446, 453, 456, 457, 462, ...], 'Office Supplies': [2, 4, 6, 8, 9, 12, 13, 14, 15, 16, 17, 18, 20, 21, 22, 25, 28, 30, 31, 32, 33, 34, 37, 42, 43, 45, 46, 49, 50, 53, 55, 56, 58, 60, 61, 63, 64, 67, 69, 70, 71, 74, 75, 77, 79, 80, 81, 82, 83, 84, 87, 88, 89, 91, 92, 94, 95, 97, 98, 99, 101, 102, 105, 108, 111, 112, 113, 114, 115, 116, 118, 120, 121, 122, 126, 127, 131, 132, 133, 134, 135, 136, 137, 138, 141, 142, 143, 144, 145, 150, 151, 153, 154, 155, 156, 158, 160, 162, 163, 164, ...], 'Technology': [7, 11, 19, 26, 35, 40, 41, 44, 47, 48, 54, 59, 62, 68, 86, 90, 100, 103, 106, 107, 109, 123, 130, 147, 148, 152, 159, 161, 165, 170, 181, 182, 183, 184, 186, 190, 205, 207, 211, 214, 215, 216, 218, 223, 235, 240, 251, 257, 258, 262, 263, 264, 265, 271, 281, 284, 291, 314, 318, 319, 320, 324, 326, 331, 335, 343, 345, 349, 373, 375, 383, 385, 386, 390, 391, 392, 401, 405, 406, 421, 427, 430, 431, 435, 436, 452, 455, 460, 473, 477, 481, 483, 486, 487, 488, 489, 493, 511, 515, 519, ...]}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df.groups"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Get unique group keys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['Furniture', 'Office Supplies', 'Technology'])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df.groups.keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Filter data on the basis of group keys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>order_id</th>\n",
       "      <th>order_date</th>\n",
       "      <th>ship_date</th>\n",
       "      <th>ship_mode</th>\n",
       "      <th>customer_id</th>\n",
       "      <th>customer_name</th>\n",
       "      <th>segment</th>\n",
       "      <th>country</th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "      <th>postal_code</th>\n",
       "      <th>region</th>\n",
       "      <th>product_id</th>\n",
       "      <th>category</th>\n",
       "      <th>sub_category</th>\n",
       "      <th>product_name</th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>CA-2015-115812</td>\n",
       "      <td>2015-06-09</td>\n",
       "      <td>2015-06-14</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>BH-11710</td>\n",
       "      <td>Brosina Hoffman</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90032.0</td>\n",
       "      <td>West</td>\n",
       "      <td>TEC-PH-10002275</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>Mitel 5320 IP Phone VoIP phone</td>\n",
       "      <td>907.152</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>12</td>\n",
       "      <td>CA-2015-115812</td>\n",
       "      <td>2015-06-09</td>\n",
       "      <td>2015-06-14</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>BH-11710</td>\n",
       "      <td>Brosina Hoffman</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90032.0</td>\n",
       "      <td>West</td>\n",
       "      <td>TEC-PH-10002033</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>Konftel 250 Conference phone - Charcoal black</td>\n",
       "      <td>911.424</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>20</td>\n",
       "      <td>CA-2015-143336</td>\n",
       "      <td>2015-08-27</td>\n",
       "      <td>2015-09-01</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>ZD-21925</td>\n",
       "      <td>Zuschuss Donatelli</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>San Francisco</td>\n",
       "      <td>California</td>\n",
       "      <td>94109.0</td>\n",
       "      <td>West</td>\n",
       "      <td>TEC-PH-10001949</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>Cisco SPA 501G IP Phone</td>\n",
       "      <td>213.480</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>27</td>\n",
       "      <td>CA-2017-121755</td>\n",
       "      <td>2017-01-16</td>\n",
       "      <td>2017-01-20</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>EH-13945</td>\n",
       "      <td>Eric Hoffmann</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90049.0</td>\n",
       "      <td>West</td>\n",
       "      <td>TEC-AC-10003027</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Accessories</td>\n",
       "      <td>Imation 8GB Mini TravelDrive USB 2.0 Flash Drive</td>\n",
       "      <td>90.570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>36</td>\n",
       "      <td>CA-2017-117590</td>\n",
       "      <td>2017-12-08</td>\n",
       "      <td>2017-12-10</td>\n",
       "      <td>First Class</td>\n",
       "      <td>GH-14485</td>\n",
       "      <td>Gene Hale</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Richardson</td>\n",
       "      <td>Texas</td>\n",
       "      <td>75080.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>TEC-PH-10004977</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>GE 30524EE4</td>\n",
       "      <td>1097.544</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9780</th>\n",
       "      <td>9781</td>\n",
       "      <td>CA-2017-153178</td>\n",
       "      <td>2017-09-14</td>\n",
       "      <td>2017-09-18</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CL-12565</td>\n",
       "      <td>Clay Ludtke</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Long Beach</td>\n",
       "      <td>New York</td>\n",
       "      <td>11561.0</td>\n",
       "      <td>East</td>\n",
       "      <td>TEC-PH-10001944</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>Wi-Ex zBoost YX540 Cellular Phone Signal Booster</td>\n",
       "      <td>437.850</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9789</th>\n",
       "      <td>9790</td>\n",
       "      <td>CA-2018-144491</td>\n",
       "      <td>2018-03-27</td>\n",
       "      <td>2018-04-01</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CJ-12010</td>\n",
       "      <td>Caroline Jumper</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Houston</td>\n",
       "      <td>Texas</td>\n",
       "      <td>77070.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>TEC-AC-10004901</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Accessories</td>\n",
       "      <td>Kensington SlimBlade Notebook Wireless Mouse w...</td>\n",
       "      <td>39.992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9797</th>\n",
       "      <td>9798</td>\n",
       "      <td>CA-2016-128608</td>\n",
       "      <td>2016-01-12</td>\n",
       "      <td>2016-01-17</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CS-12490</td>\n",
       "      <td>Cindy Schnelling</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Toledo</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>43615.0</td>\n",
       "      <td>East</td>\n",
       "      <td>TEC-PH-10004977</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>GE 30524EE4</td>\n",
       "      <td>235.188</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9798</th>\n",
       "      <td>9799</td>\n",
       "      <td>CA-2016-128608</td>\n",
       "      <td>2016-01-12</td>\n",
       "      <td>2016-01-17</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CS-12490</td>\n",
       "      <td>Cindy Schnelling</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Toledo</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>43615.0</td>\n",
       "      <td>East</td>\n",
       "      <td>TEC-PH-10000912</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>Anker 24W Portable Micro USB Car Charger</td>\n",
       "      <td>26.376</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9799</th>\n",
       "      <td>9800</td>\n",
       "      <td>CA-2016-128608</td>\n",
       "      <td>2016-01-12</td>\n",
       "      <td>2016-01-17</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CS-12490</td>\n",
       "      <td>Cindy Schnelling</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Toledo</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>43615.0</td>\n",
       "      <td>East</td>\n",
       "      <td>TEC-AC-10000487</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Accessories</td>\n",
       "      <td>SanDisk Cruzer 4 GB USB Flash Drive</td>\n",
       "      <td>10.384</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1813 rows × 18 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      row_id        order_id order_date  ship_date       ship_mode  \\\n",
       "7          8  CA-2015-115812 2015-06-09 2015-06-14  Standard Class   \n",
       "11        12  CA-2015-115812 2015-06-09 2015-06-14  Standard Class   \n",
       "19        20  CA-2015-143336 2015-08-27 2015-09-01    Second Class   \n",
       "26        27  CA-2017-121755 2017-01-16 2017-01-20    Second Class   \n",
       "35        36  CA-2017-117590 2017-12-08 2017-12-10     First Class   \n",
       "...      ...             ...        ...        ...             ...   \n",
       "9780    9781  CA-2017-153178 2017-09-14 2017-09-18  Standard Class   \n",
       "9789    9790  CA-2018-144491 2018-03-27 2018-04-01  Standard Class   \n",
       "9797    9798  CA-2016-128608 2016-01-12 2016-01-17  Standard Class   \n",
       "9798    9799  CA-2016-128608 2016-01-12 2016-01-17  Standard Class   \n",
       "9799    9800  CA-2016-128608 2016-01-12 2016-01-17  Standard Class   \n",
       "\n",
       "     customer_id       customer_name    segment        country           city  \\\n",
       "7       BH-11710     Brosina Hoffman   Consumer  United States    Los Angeles   \n",
       "11      BH-11710     Brosina Hoffman   Consumer  United States    Los Angeles   \n",
       "19      ZD-21925  Zuschuss Donatelli   Consumer  United States  San Francisco   \n",
       "26      EH-13945       Eric Hoffmann   Consumer  United States    Los Angeles   \n",
       "35      GH-14485           Gene Hale  Corporate  United States     Richardson   \n",
       "...          ...                 ...        ...            ...            ...   \n",
       "9780    CL-12565         Clay Ludtke   Consumer  United States     Long Beach   \n",
       "9789    CJ-12010     Caroline Jumper   Consumer  United States        Houston   \n",
       "9797    CS-12490    Cindy Schnelling  Corporate  United States         Toledo   \n",
       "9798    CS-12490    Cindy Schnelling  Corporate  United States         Toledo   \n",
       "9799    CS-12490    Cindy Schnelling  Corporate  United States         Toledo   \n",
       "\n",
       "           state  postal_code   region       product_id    category  \\\n",
       "7     California      90032.0     West  TEC-PH-10002275  Technology   \n",
       "11    California      90032.0     West  TEC-PH-10002033  Technology   \n",
       "19    California      94109.0     West  TEC-PH-10001949  Technology   \n",
       "26    California      90049.0     West  TEC-AC-10003027  Technology   \n",
       "35         Texas      75080.0  Central  TEC-PH-10004977  Technology   \n",
       "...          ...          ...      ...              ...         ...   \n",
       "9780    New York      11561.0     East  TEC-PH-10001944  Technology   \n",
       "9789       Texas      77070.0  Central  TEC-AC-10004901  Technology   \n",
       "9797        Ohio      43615.0     East  TEC-PH-10004977  Technology   \n",
       "9798        Ohio      43615.0     East  TEC-PH-10000912  Technology   \n",
       "9799        Ohio      43615.0     East  TEC-AC-10000487  Technology   \n",
       "\n",
       "     sub_category                                       product_name     sales  \n",
       "7          Phones                     Mitel 5320 IP Phone VoIP phone   907.152  \n",
       "11         Phones      Konftel 250 Conference phone - Charcoal black   911.424  \n",
       "19         Phones                            Cisco SPA 501G IP Phone   213.480  \n",
       "26    Accessories   Imation 8GB Mini TravelDrive USB 2.0 Flash Drive    90.570  \n",
       "35         Phones                                        GE 30524EE4  1097.544  \n",
       "...           ...                                                ...       ...  \n",
       "9780       Phones   Wi-Ex zBoost YX540 Cellular Phone Signal Booster   437.850  \n",
       "9789  Accessories  Kensington SlimBlade Notebook Wireless Mouse w...    39.992  \n",
       "9797       Phones                                        GE 30524EE4   235.188  \n",
       "9798       Phones           Anker 24W Portable Micro USB Car Charger    26.376  \n",
       "9799  Accessories                SanDisk Cruzer 4 GB USB Flash Drive    10.384  \n",
       "\n",
       "[1813 rows x 18 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Selecting a group\n",
    "\n",
    "grouped_df.get_group(\"Technology\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Returning first row, last row and nth row for each group"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>order_id</th>\n",
       "      <th>order_date</th>\n",
       "      <th>ship_date</th>\n",
       "      <th>ship_mode</th>\n",
       "      <th>customer_id</th>\n",
       "      <th>customer_name</th>\n",
       "      <th>segment</th>\n",
       "      <th>country</th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "      <th>postal_code</th>\n",
       "      <th>region</th>\n",
       "      <th>product_id</th>\n",
       "      <th>sub_category</th>\n",
       "      <th>product_name</th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>category</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Furniture</th>\n",
       "      <td>1</td>\n",
       "      <td>CA-2017-152156</td>\n",
       "      <td>2017-11-08</td>\n",
       "      <td>2017-11-11</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>CG-12520</td>\n",
       "      <td>Claire Gute</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Henderson</td>\n",
       "      <td>Kentucky</td>\n",
       "      <td>42420.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-BO-10001798</td>\n",
       "      <td>Bookcases</td>\n",
       "      <td>Bush Somerset Collection Bookcase</td>\n",
       "      <td>261.960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Office Supplies</th>\n",
       "      <td>3</td>\n",
       "      <td>CA-2017-138688</td>\n",
       "      <td>2017-06-12</td>\n",
       "      <td>2017-06-16</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>DV-13045</td>\n",
       "      <td>Darrin Van Huff</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90036.0</td>\n",
       "      <td>West</td>\n",
       "      <td>OFF-LA-10000240</td>\n",
       "      <td>Labels</td>\n",
       "      <td>Self-Adhesive Address Labels for Typewriters b...</td>\n",
       "      <td>14.620</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Technology</th>\n",
       "      <td>8</td>\n",
       "      <td>CA-2015-115812</td>\n",
       "      <td>2015-06-09</td>\n",
       "      <td>2015-06-14</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>BH-11710</td>\n",
       "      <td>Brosina Hoffman</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90032.0</td>\n",
       "      <td>West</td>\n",
       "      <td>TEC-PH-10002275</td>\n",
       "      <td>Phones</td>\n",
       "      <td>Mitel 5320 IP Phone VoIP phone</td>\n",
       "      <td>907.152</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 row_id        order_id order_date  ship_date       ship_mode  \\\n",
       "category                                                                        \n",
       "Furniture             1  CA-2017-152156 2017-11-08 2017-11-11    Second Class   \n",
       "Office Supplies       3  CA-2017-138688 2017-06-12 2017-06-16    Second Class   \n",
       "Technology            8  CA-2015-115812 2015-06-09 2015-06-14  Standard Class   \n",
       "\n",
       "                customer_id    customer_name    segment        country  \\\n",
       "category                                                                 \n",
       "Furniture          CG-12520      Claire Gute   Consumer  United States   \n",
       "Office Supplies    DV-13045  Darrin Van Huff  Corporate  United States   \n",
       "Technology         BH-11710  Brosina Hoffman   Consumer  United States   \n",
       "\n",
       "                        city       state  postal_code region       product_id  \\\n",
       "category                                                                        \n",
       "Furniture          Henderson    Kentucky      42420.0  South  FUR-BO-10001798   \n",
       "Office Supplies  Los Angeles  California      90036.0   West  OFF-LA-10000240   \n",
       "Technology       Los Angeles  California      90032.0   West  TEC-PH-10002275   \n",
       "\n",
       "                sub_category  \\\n",
       "category                       \n",
       "Furniture          Bookcases   \n",
       "Office Supplies       Labels   \n",
       "Technology            Phones   \n",
       "\n",
       "                                                      product_name    sales  \n",
       "category                                                                     \n",
       "Furniture                        Bush Somerset Collection Bookcase  261.960  \n",
       "Office Supplies  Self-Adhesive Address Labels for Typewriters b...   14.620  \n",
       "Technology                          Mitel 5320 IP Phone VoIP phone  907.152  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df.first()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>order_id</th>\n",
       "      <th>order_date</th>\n",
       "      <th>ship_date</th>\n",
       "      <th>ship_mode</th>\n",
       "      <th>customer_id</th>\n",
       "      <th>customer_name</th>\n",
       "      <th>segment</th>\n",
       "      <th>country</th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "      <th>postal_code</th>\n",
       "      <th>region</th>\n",
       "      <th>product_id</th>\n",
       "      <th>sub_category</th>\n",
       "      <th>product_name</th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>category</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Furniture</th>\n",
       "      <td>9793</td>\n",
       "      <td>CA-2015-127166</td>\n",
       "      <td>2015-05-21</td>\n",
       "      <td>2015-05-23</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>KH-16360</td>\n",
       "      <td>Katherine Hughes</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Houston</td>\n",
       "      <td>Texas</td>\n",
       "      <td>77070.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>FUR-CH-10003396</td>\n",
       "      <td>Chairs</td>\n",
       "      <td>Global Deluxe Steno Chair</td>\n",
       "      <td>107.772</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Office Supplies</th>\n",
       "      <td>9797</td>\n",
       "      <td>CA-2016-128608</td>\n",
       "      <td>2016-01-12</td>\n",
       "      <td>2016-01-17</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CS-12490</td>\n",
       "      <td>Cindy Schnelling</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Toledo</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>43615.0</td>\n",
       "      <td>East</td>\n",
       "      <td>OFF-AR-10001374</td>\n",
       "      <td>Art</td>\n",
       "      <td>BIC Brite Liner Highlighters, Chisel Tip</td>\n",
       "      <td>10.368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Technology</th>\n",
       "      <td>9800</td>\n",
       "      <td>CA-2016-128608</td>\n",
       "      <td>2016-01-12</td>\n",
       "      <td>2016-01-17</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CS-12490</td>\n",
       "      <td>Cindy Schnelling</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Toledo</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>43615.0</td>\n",
       "      <td>East</td>\n",
       "      <td>TEC-AC-10000487</td>\n",
       "      <td>Accessories</td>\n",
       "      <td>SanDisk Cruzer 4 GB USB Flash Drive</td>\n",
       "      <td>10.384</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 row_id        order_id order_date  ship_date       ship_mode  \\\n",
       "category                                                                        \n",
       "Furniture          9793  CA-2015-127166 2015-05-21 2015-05-23    Second Class   \n",
       "Office Supplies    9797  CA-2016-128608 2016-01-12 2016-01-17  Standard Class   \n",
       "Technology         9800  CA-2016-128608 2016-01-12 2016-01-17  Standard Class   \n",
       "\n",
       "                customer_id     customer_name    segment        country  \\\n",
       "category                                                                  \n",
       "Furniture          KH-16360  Katherine Hughes   Consumer  United States   \n",
       "Office Supplies    CS-12490  Cindy Schnelling  Corporate  United States   \n",
       "Technology         CS-12490  Cindy Schnelling  Corporate  United States   \n",
       "\n",
       "                    city  state  postal_code   region       product_id  \\\n",
       "category                                                                 \n",
       "Furniture        Houston  Texas      77070.0  Central  FUR-CH-10003396   \n",
       "Office Supplies   Toledo   Ohio      43615.0     East  OFF-AR-10001374   \n",
       "Technology        Toledo   Ohio      43615.0     East  TEC-AC-10000487   \n",
       "\n",
       "                sub_category                              product_name  \\\n",
       "category                                                                 \n",
       "Furniture             Chairs                 Global Deluxe Steno Chair   \n",
       "Office Supplies          Art  BIC Brite Liner Highlighters, Chisel Tip   \n",
       "Technology       Accessories       SanDisk Cruzer 4 GB USB Flash Drive   \n",
       "\n",
       "                   sales  \n",
       "category                  \n",
       "Furniture        107.772  \n",
       "Office Supplies   10.368  \n",
       "Technology        10.384  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df.last()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>order_id</th>\n",
       "      <th>order_date</th>\n",
       "      <th>ship_date</th>\n",
       "      <th>ship_mode</th>\n",
       "      <th>customer_id</th>\n",
       "      <th>customer_name</th>\n",
       "      <th>segment</th>\n",
       "      <th>country</th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "      <th>postal_code</th>\n",
       "      <th>region</th>\n",
       "      <th>product_id</th>\n",
       "      <th>sub_category</th>\n",
       "      <th>product_name</th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>category</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Furniture</th>\n",
       "      <td>39</td>\n",
       "      <td>CA-2016-117415</td>\n",
       "      <td>2016-12-27</td>\n",
       "      <td>2016-12-31</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>SN-20710</td>\n",
       "      <td>Steve Nguyen</td>\n",
       "      <td>Home Office</td>\n",
       "      <td>United States</td>\n",
       "      <td>Houston</td>\n",
       "      <td>Texas</td>\n",
       "      <td>77041.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>FUR-BO-10002545</td>\n",
       "      <td>Bookcases</td>\n",
       "      <td>Atlantic Metals Mobile 3-Shelf Bookcases, Cust...</td>\n",
       "      <td>532.3992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Office Supplies</th>\n",
       "      <td>18</td>\n",
       "      <td>CA-2015-167164</td>\n",
       "      <td>2015-05-13</td>\n",
       "      <td>2015-05-15</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>AG-10270</td>\n",
       "      <td>Alejandro Grove</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>West Jordan</td>\n",
       "      <td>Utah</td>\n",
       "      <td>84084.0</td>\n",
       "      <td>West</td>\n",
       "      <td>OFF-ST-10000107</td>\n",
       "      <td>Storage</td>\n",
       "      <td>Fellowes Super Stor/Drawer</td>\n",
       "      <td>55.5000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Technology</th>\n",
       "      <td>55</td>\n",
       "      <td>CA-2017-105816</td>\n",
       "      <td>2017-12-11</td>\n",
       "      <td>2017-12-17</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>JM-15265</td>\n",
       "      <td>Janet Molinari</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>New York City</td>\n",
       "      <td>New York</td>\n",
       "      <td>10024.0</td>\n",
       "      <td>East</td>\n",
       "      <td>TEC-PH-10002447</td>\n",
       "      <td>Phones</td>\n",
       "      <td>AT&amp;T CL83451 4-Handset Telephone</td>\n",
       "      <td>1029.9500</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 row_id        order_id order_date  ship_date       ship_mode  \\\n",
       "category                                                                        \n",
       "Furniture            39  CA-2016-117415 2016-12-27 2016-12-31  Standard Class   \n",
       "Office Supplies      18  CA-2015-167164 2015-05-13 2015-05-15    Second Class   \n",
       "Technology           55  CA-2017-105816 2017-12-11 2017-12-17  Standard Class   \n",
       "\n",
       "                customer_id    customer_name      segment        country  \\\n",
       "category                                                                   \n",
       "Furniture          SN-20710     Steve Nguyen  Home Office  United States   \n",
       "Office Supplies    AG-10270  Alejandro Grove     Consumer  United States   \n",
       "Technology         JM-15265   Janet Molinari    Corporate  United States   \n",
       "\n",
       "                          city     state  postal_code   region  \\\n",
       "category                                                         \n",
       "Furniture              Houston     Texas      77041.0  Central   \n",
       "Office Supplies    West Jordan      Utah      84084.0     West   \n",
       "Technology       New York City  New York      10024.0     East   \n",
       "\n",
       "                      product_id sub_category  \\\n",
       "category                                        \n",
       "Furniture        FUR-BO-10002545    Bookcases   \n",
       "Office Supplies  OFF-ST-10000107      Storage   \n",
       "Technology       TEC-PH-10002447       Phones   \n",
       "\n",
       "                                                      product_name      sales  \n",
       "category                                                                       \n",
       "Furniture        Atlantic Metals Mobile 3-Shelf Bookcases, Cust...   532.3992  \n",
       "Office Supplies                         Fellowes Super Stor/Drawer    55.5000  \n",
       "Technology                        AT&T CL83451 4-Handset Telephone  1029.9500  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df.nth(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Grouping the Data Based on Product Category and Sub-Category"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Grouping based on category first and then sub_category\n",
    "\n",
    "grouped_df = df.groupby(['category', 'sub_category'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Returning all the groups and row indexes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{('Furniture', 'Bookcases'): [0, 27, 38, 189, 192, 213, 292, 354, 369, 399, 412, 468, 472, 485, 688, 708, 736, 783, 841, 906, 954, 1042, 1114, 1211, 1247, 1302, 1369, 1386, 1534, 1539, 1545, 1594, 1610, 1714, 1723, 1760, 1762, 1860, 1875, 1932, 2007, 2025, 2115, 2122, 2225, 2262, 2281, 2305, 2326, 2353, 2403, 2415, 2471, 2543, 2546, 2558, 2603, 2650, 2654, 2737, 2777, 2796, 2808, 2825, 2860, 3023, 3030, 3074, 3098, 3100, 3102, 3175, 3351, 3365, 3368, 3466, 3507, 3512, 3762, 3820, 3845, 3910, 3928, 3985, 3994, 3999, 4023, 4071, 4088, 4110, 4184, 4217, 4223, 4266, 4284, 4383, 4385, 4389, 4423, 4453, ...], ('Furniture', 'Chairs'): [1, 23, 39, 52, 57, 66, 72, 85, 124, 128, 149, 157, 167, 173, 177, 228, 229, 244, 249, 294, 310, 317, 328, 362, 413, 415, 417, 424, 439, 444, 456, 457, 498, 502, 526, 531, 539, 551, 569, 586, 622, 635, 657, 730, 769, 777, 787, 791, 799, 819, 829, 847, 880, 916, 960, 980, 983, 990, 1021, 1030, 1045, 1060, 1067, 1081, 1126, 1158, 1177, 1190, 1198, 1200, 1202, 1212, 1249, 1267, 1274, 1310, 1313, 1350, 1359, 1367, 1411, 1433, 1438, 1446, 1463, 1468, 1492, 1515, 1570, 1573, 1575, 1595, 1627, 1701, 1709, 1791, 1822, 1832, 1841, 1847, ...], ('Furniture', 'Furnishings'): [5, 29, 36, 51, 65, 73, 76, 78, 93, 96, 104, 110, 119, 129, 139, 140, 146, 204, 222, 234, 238, 239, 242, 254, 272, 293, 295, 301, 304, 309, 311, 313, 325, 364, 387, 422, 425, 440, 446, 462, 467, 478, 497, 499, 510, 527, 532, 545, 552, 559, 562, 579, 597, 603, 616, 617, 620, 626, 638, 640, 648, 668, 669, 678, 689, 701, 702, 716, 717, 719, 723, 750, 754, 768, 775, 800, 804, 807, 813, 848, 853, 866, 875, 883, 884, 912, 913, 947, 948, 952, 959, 970, 988, 998, 999, 1010, 1011, 1016, 1017, 1031, ...], ('Furniture', 'Tables'): [3, 10, 24, 117, 125, 201, 226, 231, 232, 241, 282, 303, 338, 377, 384, 408, 453, 463, 494, 522, 557, 661, 670, 703, 721, 728, 746, 755, 810, 915, 942, 946, 949, 1002, 1013, 1040, 1082, 1097, 1129, 1155, 1157, 1245, 1246, 1354, 1374, 1394, 1395, 1402, 1405, 1409, 1456, 1461, 1505, 1516, 1558, 1562, 1580, 1669, 1689, 1699, 1713, 1727, 1750, 1811, 1814, 1831, 1863, 1872, 1873, 1889, 1899, 1948, 1988, 2058, 2069, 2091, 2116, 2141, 2228, 2274, 2277, 2289, 2347, 2357, 2358, 2359, 2413, 2425, 2437, 2441, 2477, 2483, 2493, 2567, 2591, 2609, 2664, 2726, 2746, 2812, ...], ('Office Supplies', 'Appliances'): [9, 14, 22, 79, 98, 144, 151, 169, 174, 176, 202, 203, 247, 261, 287, 359, 378, 404, 459, 466, 538, 561, 583, 594, 623, 627, 637, 645, 647, 654, 673, 676, 684, 705, 712, 802, 811, 830, 834, 923, 928, 936, 939, 1044, 1061, 1077, 1094, 1137, 1146, 1159, 1188, 1250, 1271, 1295, 1308, 1322, 1333, 1345, 1375, 1421, 1536, 1550, 1578, 1626, 1684, 1697, 1711, 1716, 1724, 1725, 1726, 1734, 1818, 1820, 1836, 1839, 1858, 1888, 1949, 1959, 1965, 1983, 1995, 2009, 2045, 2077, 2129, 2190, 2192, 2194, 2211, 2215, 2230, 2245, 2250, 2253, 2254, 2284, 2286, 2287, ...], ('Office Supplies', 'Art'): [6, 18, 21, 31, 33, 61, 67, 81, 89, 108, 111, 135, 141, 156, 164, 168, 179, 193, 195, 196, 224, 225, 243, 266, 275, 305, 307, 308, 316, 340, 346, 348, 356, 363, 374, 388, 403, 416, 419, 428, 447, 449, 450, 451, 479, 482, 490, 506, 507, 512, 513, 514, 555, 574, 592, 608, 625, 672, 694, 698, 751, 774, 776, 805, 809, 833, 839, 840, 850, 852, 861, 863, 882, 892, 927, 929, 930, 953, 965, 981, 989, 1000, 1020, 1038, 1039, 1043, 1053, 1063, 1070, 1076, 1091, 1098, 1101, 1122, 1124, 1127, 1139, 1152, 1161, 1181, ...], ('Office Supplies', 'Binders'): [8, 13, 15, 20, 25, 28, 32, 45, 49, 60, 63, 70, 75, 80, 94, 95, 97, 101, 105, 112, 118, 120, 126, 137, 150, 163, 185, 198, 208, 221, 230, 233, 260, 273, 280, 286, 296, 302, 306, 323, 330, 332, 333, 334, 336, 337, 341, 351, 353, 366, 372, 380, 382, 389, 393, 394, 426, 429, 434, 461, 464, 469, 496, 500, 501, 503, 505, 509, 521, 534, 536, 537, 546, 549, 560, 567, 572, 584, 590, 604, 605, 607, 615, 618, 621, 628, 636, 639, 655, 658, 662, 679, 680, 699, 700, 709, 715, 718, 734, 735, ...], ('Office Supplies', 'Envelopes'): [30, 37, 83, 114, 116, 122, 142, 162, 194, 253, 269, 355, 420, 458, 492, 495, 530, 656, 696, 711, 725, 763, 764, 785, 828, 896, 918, 962, 985, 996, 1006, 1023, 1109, 1120, 1125, 1128, 1131, 1136, 1182, 1228, 1232, 1244, 1387, 1401, 1427, 1490, 1556, 1571, 1852, 1880, 1892, 1897, 1989, 2124, 2187, 2328, 2335, 2402, 2434, 2444, 2446, 2497, 2499, 2516, 2574, 2576, 2585, 2594, 2625, 2658, 2661, 2685, 2741, 2827, 2877, 2911, 2935, 2948, 2996, 3042, 3056, 3113, 3137, 3161, 3162, 3186, 3210, 3220, 3225, 3266, 3304, 3381, 3382, 3442, 3606, 3657, 3672, 3689, 3695, 3741, ...], ('Office Supplies', 'Fasteners'): [53, 113, 132, 136, 209, 219, 267, 289, 297, 322, 339, 342, 525, 553, 563, 571, 646, 664, 826, 859, 992, 1059, 1062, 1065, 1108, 1142, 1147, 1208, 1240, 1243, 1384, 1464, 1485, 1528, 1532, 1542, 1587, 1591, 1642, 1647, 1654, 1665, 1703, 1799, 1821, 1886, 1973, 1994, 2059, 2088, 2110, 2134, 2144, 2155, 2226, 2244, 2259, 2372, 2375, 2379, 2387, 2410, 2426, 2532, 2701, 2720, 2786, 2806, 2874, 2909, 2918, 2923, 2959, 3019, 3095, 3195, 3223, 3250, 3270, 3274, 3345, 3386, 3415, 3451, 3535, 3565, 3649, 3677, 3716, 3744, 3769, 3773, 3789, 3835, 3896, 3898, 3977, 4065, 4077, 4133, ...], ('Office Supplies', 'Labels'): [2, 50, 87, 158, 175, 212, 279, 290, 312, 329, 360, 361, 410, 445, 484, 556, 564, 570, 587, 611, 634, 642, 686, 695, 697, 731, 739, 761, 793, 798, 823, 845, 865, 867, 897, 900, 963, 964, 968, 973, 984, 1032, 1088, 1116, 1134, 1176, 1281, 1294, 1311, 1312, 1344, 1372, 1410, 1429, 1458, 1472, 1476, 1489, 1552, 1560, 1615, 1616, 1621, 1673, 1698, 1706, 1729, 1740, 1764, 1766, 1776, 1815, 1817, 1837, 1842, 1884, 1914, 1916, 1941, 1952, 1992, 1997, 2017, 2037, 2083, 2084, 2085, 2089, 2123, 2180, 2313, 2397, 2430, 2450, 2456, 2465, 2475, 2503, 2518, 2519, ...], ('Office Supplies', 'Paper'): [12, 34, 56, 58, 64, 69, 71, 91, 92, 99, 102, 115, 131, 133, 134, 143, 153, 154, 160, 171, 172, 188, 191, 199, 200, 220, 236, 237, 246, 248, 250, 274, 276, 277, 283, 285, 298, 300, 315, 347, 350, 352, 357, 358, 370, 376, 381, 396, 402, 407, 409, 411, 414, 418, 437, 438, 443, 470, 475, 476, 504, 517, 547, 558, 566, 568, 575, 576, 589, 591, 606, 612, 629, 644, 651, 652, 666, 671, 674, 687, 693, 704, 710, 720, 727, 743, 744, 759, 760, 778, 779, 780, 794, 796, 797, 803, 815, 816, 817, 821, ...], ('Office Supplies', 'Storage'): [4, 16, 17, 42, 43, 46, 55, 74, 77, 82, 84, 88, 121, 127, 145, 155, 166, 180, 187, 197, 206, 210, 217, 227, 245, 252, 255, 256, 259, 268, 270, 278, 299, 321, 327, 344, 365, 368, 371, 379, 395, 398, 400, 423, 432, 433, 441, 442, 448, 454, 471, 474, 480, 491, 508, 516, 518, 520, 528, 535, 542, 548, 554, 578, 602, 624, 630, 641, 659, 660, 663, 681, 682, 691, 722, 729, 732, 740, 747, 756, 767, 789, 854, 857, 881, 886, 894, 904, 911, 919, 920, 944, 955, 969, 997, 1004, 1012, 1019, 1078, 1084, ...], ('Office Supplies', 'Supplies'): [138, 178, 288, 367, 397, 465, 529, 550, 577, 580, 598, 601, 665, 724, 742, 745, 827, 844, 856, 860, 1068, 1175, 1292, 1316, 1404, 1432, 1477, 1598, 1763, 1769, 1803, 1967, 2018, 2093, 2112, 2140, 2237, 2377, 2479, 2505, 2600, 2705, 2739, 2866, 2867, 2881, 3093, 3101, 3117, 3226, 3280, 3292, 3418, 3531, 3547, 3588, 3591, 3647, 3660, 3667, 3717, 3750, 3759, 3803, 3843, 3844, 3943, 3946, 3990, 3992, 4064, 4090, 4420, 4458, 4488, 4590, 4597, 4622, 4649, 4704, 4742, 4747, 4762, 4770, 4777, 4782, 4901, 4979, 5001, 5019, 5020, 5030, 5142, 5198, 5241, 5312, 5332, 5448, 5452, 5492, ...], ('Technology', 'Accessories'): [26, 44, 47, 59, 62, 86, 100, 103, 106, 109, 161, 181, 184, 186, 211, 216, 235, 251, 258, 265, 271, 284, 291, 320, 324, 349, 373, 375, 383, 385, 390, 401, 405, 421, 430, 431, 435, 455, 460, 481, 483, 489, 493, 511, 519, 524, 540, 543, 593, 631, 633, 643, 650, 653, 675, 677, 685, 692, 706, 707, 714, 733, 738, 757, 765, 782, 784, 808, 812, 814, 822, 824, 825, 849, 862, 876, 877, 889, 910, 914, 921, 926, 937, 1001, 1014, 1041, 1049, 1075, 1100, 1115, 1118, 1166, 1179, 1197, 1216, 1218, 1223, 1259, 1276, 1278, ...], ('Technology', 'Copiers'): [335, 392, 406, 515, 595, 753, 1150, 1233, 1549, 1644, 1986, 2273, 2386, 2623, 2897, 2899, 3055, 3273, 3691, 3704, 3749, 3824, 3983, 4072, 4190, 4200, 4286, 4491, 4572, 4619, 4639, 4812, 5068, 5562, 5568, 5710, 5756, 5772, 5850, 6062, 6200, 6425, 6527, 6649, 6826, 6901, 7036, 7042, 7173, 7460, 7472, 7520, 7666, 7697, 8123, 8153, 8178, 8253, 8493, 8539, 8552, 8570, 8799, 8820, 8990, 9617], ('Technology', 'Machines'): [165, 215, 223, 262, 263, 318, 386, 427, 436, 683, 835, 977, 986, 1022, 1085, 1144, 1340, 1364, 1454, 1507, 1681, 1833, 1912, 2182, 2356, 2418, 2602, 2628, 2682, 2696, 2697, 2709, 2838, 2999, 3011, 3112, 3151, 3295, 3298, 3424, 3510, 3532, 3569, 3678, 3690, 4003, 4029, 4039, 4093, 4128, 4134, 4218, 4221, 4277, 4312, 4644, 4750, 4823, 5006, 5118, 5126, 5268, 5380, 5565, 5741, 5758, 5780, 6010, 6041, 6069, 6323, 6340, 6363, 6484, 6574, 6608, 6626, 6675, 6764, 6800, 6810, 6814, 6951, 7206, 7237, 7281, 7288, 7292, 7328, 7422, 7600, 7641, 7646, 7772, 7914, 8100, 8102, 8204, 8282, 8477, ...], ('Technology', 'Phones'): [7, 11, 19, 35, 40, 41, 48, 54, 68, 90, 107, 123, 130, 147, 148, 152, 159, 170, 182, 183, 190, 205, 207, 214, 218, 240, 257, 264, 281, 314, 319, 326, 331, 343, 345, 391, 452, 473, 477, 486, 487, 488, 523, 533, 541, 544, 565, 573, 581, 582, 585, 588, 596, 599, 600, 609, 610, 613, 614, 619, 632, 649, 667, 690, 713, 726, 748, 762, 771, 792, 801, 806, 831, 832, 838, 846, 864, 870, 893, 902, 907, 909, 922, 956, 971, 974, 982, 995, 1009, 1018, 1027, 1029, 1046, 1048, 1064, 1087, 1090, 1092, 1110, 1121, ...]}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Returning each group and row ids associated to the group\n",
    "\n",
    "grouped_df.groups"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Get unique group keys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys([('Furniture', 'Bookcases'), ('Furniture', 'Chairs'), ('Furniture', 'Furnishings'), ('Furniture', 'Tables'), ('Office Supplies', 'Appliances'), ('Office Supplies', 'Art'), ('Office Supplies', 'Binders'), ('Office Supplies', 'Envelopes'), ('Office Supplies', 'Fasteners'), ('Office Supplies', 'Labels'), ('Office Supplies', 'Paper'), ('Office Supplies', 'Storage'), ('Office Supplies', 'Supplies'), ('Technology', 'Accessories'), ('Technology', 'Copiers'), ('Technology', 'Machines'), ('Technology', 'Phones')])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df.groups.keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Filter data on the basis of group keys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>order_id</th>\n",
       "      <th>order_date</th>\n",
       "      <th>ship_date</th>\n",
       "      <th>ship_mode</th>\n",
       "      <th>customer_id</th>\n",
       "      <th>customer_name</th>\n",
       "      <th>segment</th>\n",
       "      <th>country</th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "      <th>postal_code</th>\n",
       "      <th>region</th>\n",
       "      <th>product_id</th>\n",
       "      <th>category</th>\n",
       "      <th>sub_category</th>\n",
       "      <th>product_name</th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>CA-2015-115812</td>\n",
       "      <td>2015-06-09</td>\n",
       "      <td>2015-06-14</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>BH-11710</td>\n",
       "      <td>Brosina Hoffman</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90032.0</td>\n",
       "      <td>West</td>\n",
       "      <td>TEC-PH-10002275</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>Mitel 5320 IP Phone VoIP phone</td>\n",
       "      <td>907.152</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>12</td>\n",
       "      <td>CA-2015-115812</td>\n",
       "      <td>2015-06-09</td>\n",
       "      <td>2015-06-14</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>BH-11710</td>\n",
       "      <td>Brosina Hoffman</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90032.0</td>\n",
       "      <td>West</td>\n",
       "      <td>TEC-PH-10002033</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>Konftel 250 Conference phone - Charcoal black</td>\n",
       "      <td>911.424</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>20</td>\n",
       "      <td>CA-2015-143336</td>\n",
       "      <td>2015-08-27</td>\n",
       "      <td>2015-09-01</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>ZD-21925</td>\n",
       "      <td>Zuschuss Donatelli</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>San Francisco</td>\n",
       "      <td>California</td>\n",
       "      <td>94109.0</td>\n",
       "      <td>West</td>\n",
       "      <td>TEC-PH-10001949</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>Cisco SPA 501G IP Phone</td>\n",
       "      <td>213.480</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>36</td>\n",
       "      <td>CA-2017-117590</td>\n",
       "      <td>2017-12-08</td>\n",
       "      <td>2017-12-10</td>\n",
       "      <td>First Class</td>\n",
       "      <td>GH-14485</td>\n",
       "      <td>Gene Hale</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Richardson</td>\n",
       "      <td>Texas</td>\n",
       "      <td>75080.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>TEC-PH-10004977</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>GE 30524EE4</td>\n",
       "      <td>1097.544</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>41</td>\n",
       "      <td>CA-2016-117415</td>\n",
       "      <td>2016-12-27</td>\n",
       "      <td>2016-12-31</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>SN-20710</td>\n",
       "      <td>Steve Nguyen</td>\n",
       "      <td>Home Office</td>\n",
       "      <td>United States</td>\n",
       "      <td>Houston</td>\n",
       "      <td>Texas</td>\n",
       "      <td>77041.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>TEC-PH-10000486</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>Plantronics HL10 Handset Lifter</td>\n",
       "      <td>371.168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9764</th>\n",
       "      <td>9765</td>\n",
       "      <td>CA-2015-123855</td>\n",
       "      <td>2015-06-18</td>\n",
       "      <td>2015-06-23</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>MC-18100</td>\n",
       "      <td>Mick Crebagga</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90036.0</td>\n",
       "      <td>West</td>\n",
       "      <td>TEC-PH-10000215</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>Plantronics Cordless Phone Headset with In-lin...</td>\n",
       "      <td>139.800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9773</th>\n",
       "      <td>9774</td>\n",
       "      <td>CA-2017-160234</td>\n",
       "      <td>2017-06-26</td>\n",
       "      <td>2017-07-03</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>PF-19225</td>\n",
       "      <td>Phillip Flathmann</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Atlanta</td>\n",
       "      <td>Georgia</td>\n",
       "      <td>30318.0</td>\n",
       "      <td>South</td>\n",
       "      <td>TEC-PH-10004434</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>Cisco IP Phone 7961G VoIP phone - Dark gray</td>\n",
       "      <td>135.950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9780</th>\n",
       "      <td>9781</td>\n",
       "      <td>CA-2017-153178</td>\n",
       "      <td>2017-09-14</td>\n",
       "      <td>2017-09-18</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CL-12565</td>\n",
       "      <td>Clay Ludtke</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Long Beach</td>\n",
       "      <td>New York</td>\n",
       "      <td>11561.0</td>\n",
       "      <td>East</td>\n",
       "      <td>TEC-PH-10001944</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>Wi-Ex zBoost YX540 Cellular Phone Signal Booster</td>\n",
       "      <td>437.850</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9797</th>\n",
       "      <td>9798</td>\n",
       "      <td>CA-2016-128608</td>\n",
       "      <td>2016-01-12</td>\n",
       "      <td>2016-01-17</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CS-12490</td>\n",
       "      <td>Cindy Schnelling</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Toledo</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>43615.0</td>\n",
       "      <td>East</td>\n",
       "      <td>TEC-PH-10004977</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>GE 30524EE4</td>\n",
       "      <td>235.188</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9798</th>\n",
       "      <td>9799</td>\n",
       "      <td>CA-2016-128608</td>\n",
       "      <td>2016-01-12</td>\n",
       "      <td>2016-01-17</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CS-12490</td>\n",
       "      <td>Cindy Schnelling</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Toledo</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>43615.0</td>\n",
       "      <td>East</td>\n",
       "      <td>TEC-PH-10000912</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>Anker 24W Portable Micro USB Car Charger</td>\n",
       "      <td>26.376</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>876 rows × 18 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      row_id        order_id order_date  ship_date       ship_mode  \\\n",
       "7          8  CA-2015-115812 2015-06-09 2015-06-14  Standard Class   \n",
       "11        12  CA-2015-115812 2015-06-09 2015-06-14  Standard Class   \n",
       "19        20  CA-2015-143336 2015-08-27 2015-09-01    Second Class   \n",
       "35        36  CA-2017-117590 2017-12-08 2017-12-10     First Class   \n",
       "40        41  CA-2016-117415 2016-12-27 2016-12-31  Standard Class   \n",
       "...      ...             ...        ...        ...             ...   \n",
       "9764    9765  CA-2015-123855 2015-06-18 2015-06-23  Standard Class   \n",
       "9773    9774  CA-2017-160234 2017-06-26 2017-07-03  Standard Class   \n",
       "9780    9781  CA-2017-153178 2017-09-14 2017-09-18  Standard Class   \n",
       "9797    9798  CA-2016-128608 2016-01-12 2016-01-17  Standard Class   \n",
       "9798    9799  CA-2016-128608 2016-01-12 2016-01-17  Standard Class   \n",
       "\n",
       "     customer_id       customer_name      segment        country  \\\n",
       "7       BH-11710     Brosina Hoffman     Consumer  United States   \n",
       "11      BH-11710     Brosina Hoffman     Consumer  United States   \n",
       "19      ZD-21925  Zuschuss Donatelli     Consumer  United States   \n",
       "35      GH-14485           Gene Hale    Corporate  United States   \n",
       "40      SN-20710        Steve Nguyen  Home Office  United States   \n",
       "...          ...                 ...          ...            ...   \n",
       "9764    MC-18100       Mick Crebagga     Consumer  United States   \n",
       "9773    PF-19225   Phillip Flathmann     Consumer  United States   \n",
       "9780    CL-12565         Clay Ludtke     Consumer  United States   \n",
       "9797    CS-12490    Cindy Schnelling    Corporate  United States   \n",
       "9798    CS-12490    Cindy Schnelling    Corporate  United States   \n",
       "\n",
       "               city       state  postal_code   region       product_id  \\\n",
       "7       Los Angeles  California      90032.0     West  TEC-PH-10002275   \n",
       "11      Los Angeles  California      90032.0     West  TEC-PH-10002033   \n",
       "19    San Francisco  California      94109.0     West  TEC-PH-10001949   \n",
       "35       Richardson       Texas      75080.0  Central  TEC-PH-10004977   \n",
       "40          Houston       Texas      77041.0  Central  TEC-PH-10000486   \n",
       "...             ...         ...          ...      ...              ...   \n",
       "9764    Los Angeles  California      90036.0     West  TEC-PH-10000215   \n",
       "9773        Atlanta     Georgia      30318.0    South  TEC-PH-10004434   \n",
       "9780     Long Beach    New York      11561.0     East  TEC-PH-10001944   \n",
       "9797         Toledo        Ohio      43615.0     East  TEC-PH-10004977   \n",
       "9798         Toledo        Ohio      43615.0     East  TEC-PH-10000912   \n",
       "\n",
       "        category sub_category  \\\n",
       "7     Technology       Phones   \n",
       "11    Technology       Phones   \n",
       "19    Technology       Phones   \n",
       "35    Technology       Phones   \n",
       "40    Technology       Phones   \n",
       "...          ...          ...   \n",
       "9764  Technology       Phones   \n",
       "9773  Technology       Phones   \n",
       "9780  Technology       Phones   \n",
       "9797  Technology       Phones   \n",
       "9798  Technology       Phones   \n",
       "\n",
       "                                           product_name     sales  \n",
       "7                        Mitel 5320 IP Phone VoIP phone   907.152  \n",
       "11        Konftel 250 Conference phone - Charcoal black   911.424  \n",
       "19                              Cisco SPA 501G IP Phone   213.480  \n",
       "35                                          GE 30524EE4  1097.544  \n",
       "40                      Plantronics HL10 Handset Lifter   371.168  \n",
       "...                                                 ...       ...  \n",
       "9764  Plantronics Cordless Phone Headset with In-lin...   139.800  \n",
       "9773        Cisco IP Phone 7961G VoIP phone - Dark gray   135.950  \n",
       "9780   Wi-Ex zBoost YX540 Cellular Phone Signal Booster   437.850  \n",
       "9797                                        GE 30524EE4   235.188  \n",
       "9798           Anker 24W Portable Micro USB Car Charger    26.376  \n",
       "\n",
       "[876 rows x 18 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df.get_group(('Technology', 'Phones'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Returning first row, last row and nth row for each group"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>order_id</th>\n",
       "      <th>order_date</th>\n",
       "      <th>ship_date</th>\n",
       "      <th>ship_mode</th>\n",
       "      <th>customer_id</th>\n",
       "      <th>customer_name</th>\n",
       "      <th>segment</th>\n",
       "      <th>country</th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "      <th>postal_code</th>\n",
       "      <th>region</th>\n",
       "      <th>product_id</th>\n",
       "      <th>product_name</th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>category</th>\n",
       "      <th>sub_category</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Furniture</th>\n",
       "      <th>Bookcases</th>\n",
       "      <td>1</td>\n",
       "      <td>CA-2017-152156</td>\n",
       "      <td>2017-11-08</td>\n",
       "      <td>2017-11-11</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>CG-12520</td>\n",
       "      <td>Claire Gute</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Henderson</td>\n",
       "      <td>Kentucky</td>\n",
       "      <td>42420.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-BO-10001798</td>\n",
       "      <td>Bush Somerset Collection Bookcase</td>\n",
       "      <td>261.9600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chairs</th>\n",
       "      <td>2</td>\n",
       "      <td>CA-2017-152156</td>\n",
       "      <td>2017-11-08</td>\n",
       "      <td>2017-11-11</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>CG-12520</td>\n",
       "      <td>Claire Gute</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Henderson</td>\n",
       "      <td>Kentucky</td>\n",
       "      <td>42420.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-CH-10000454</td>\n",
       "      <td>Hon Deluxe Fabric Upholstered Stacking Chairs,...</td>\n",
       "      <td>731.9400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Furnishings</th>\n",
       "      <td>6</td>\n",
       "      <td>CA-2015-115812</td>\n",
       "      <td>2015-06-09</td>\n",
       "      <td>2015-06-14</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>BH-11710</td>\n",
       "      <td>Brosina Hoffman</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90032.0</td>\n",
       "      <td>West</td>\n",
       "      <td>FUR-FU-10001487</td>\n",
       "      <td>Eldon Expressions Wood and Plastic Desk Access...</td>\n",
       "      <td>48.8600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tables</th>\n",
       "      <td>4</td>\n",
       "      <td>US-2016-108966</td>\n",
       "      <td>2016-10-11</td>\n",
       "      <td>2016-10-18</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>SO-20335</td>\n",
       "      <td>Sean O'Donnell</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Fort Lauderdale</td>\n",
       "      <td>Florida</td>\n",
       "      <td>33311.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-TA-10000577</td>\n",
       "      <td>Bretford CR4500 Series Slim Rectangular Table</td>\n",
       "      <td>957.5775</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"9\" valign=\"top\">Office Supplies</th>\n",
       "      <th>Appliances</th>\n",
       "      <td>10</td>\n",
       "      <td>CA-2015-115812</td>\n",
       "      <td>2015-06-09</td>\n",
       "      <td>2015-06-14</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>BH-11710</td>\n",
       "      <td>Brosina Hoffman</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90032.0</td>\n",
       "      <td>West</td>\n",
       "      <td>OFF-AP-10002892</td>\n",
       "      <td>Belkin F5C206VTEL 6 Outlet Surge</td>\n",
       "      <td>114.9000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Art</th>\n",
       "      <td>7</td>\n",
       "      <td>CA-2015-115812</td>\n",
       "      <td>2015-06-09</td>\n",
       "      <td>2015-06-14</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>BH-11710</td>\n",
       "      <td>Brosina Hoffman</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90032.0</td>\n",
       "      <td>West</td>\n",
       "      <td>OFF-AR-10002833</td>\n",
       "      <td>Newell 322</td>\n",
       "      <td>7.2800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Binders</th>\n",
       "      <td>9</td>\n",
       "      <td>CA-2015-115812</td>\n",
       "      <td>2015-06-09</td>\n",
       "      <td>2015-06-14</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>BH-11710</td>\n",
       "      <td>Brosina Hoffman</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90032.0</td>\n",
       "      <td>West</td>\n",
       "      <td>OFF-BI-10003910</td>\n",
       "      <td>DXL Angle-View Binders with Locking Rings by S...</td>\n",
       "      <td>18.5040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Envelopes</th>\n",
       "      <td>31</td>\n",
       "      <td>US-2016-150630</td>\n",
       "      <td>2016-09-17</td>\n",
       "      <td>2016-09-21</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>TB-21520</td>\n",
       "      <td>Tracy Blumstein</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Philadelphia</td>\n",
       "      <td>Pennsylvania</td>\n",
       "      <td>19140.0</td>\n",
       "      <td>East</td>\n",
       "      <td>OFF-EN-10001509</td>\n",
       "      <td>Poly String Tie Envelopes</td>\n",
       "      <td>3.2640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Fasteners</th>\n",
       "      <td>54</td>\n",
       "      <td>CA-2017-105816</td>\n",
       "      <td>2017-12-11</td>\n",
       "      <td>2017-12-17</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>JM-15265</td>\n",
       "      <td>Janet Molinari</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>New York City</td>\n",
       "      <td>New York</td>\n",
       "      <td>10024.0</td>\n",
       "      <td>East</td>\n",
       "      <td>OFF-FA-10000304</td>\n",
       "      <td>Advantus Push Pins</td>\n",
       "      <td>15.2600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Labels</th>\n",
       "      <td>3</td>\n",
       "      <td>CA-2017-138688</td>\n",
       "      <td>2017-06-12</td>\n",
       "      <td>2017-06-16</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>DV-13045</td>\n",
       "      <td>Darrin Van Huff</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90036.0</td>\n",
       "      <td>West</td>\n",
       "      <td>OFF-LA-10000240</td>\n",
       "      <td>Self-Adhesive Address Labels for Typewriters b...</td>\n",
       "      <td>14.6200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Paper</th>\n",
       "      <td>13</td>\n",
       "      <td>CA-2018-114412</td>\n",
       "      <td>2018-04-15</td>\n",
       "      <td>2018-04-20</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>AA-10480</td>\n",
       "      <td>Andrew Allen</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Concord</td>\n",
       "      <td>North Carolina</td>\n",
       "      <td>28027.0</td>\n",
       "      <td>South</td>\n",
       "      <td>OFF-PA-10002365</td>\n",
       "      <td>Xerox 1967</td>\n",
       "      <td>15.5520</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Storage</th>\n",
       "      <td>5</td>\n",
       "      <td>US-2016-108966</td>\n",
       "      <td>2016-10-11</td>\n",
       "      <td>2016-10-18</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>SO-20335</td>\n",
       "      <td>Sean O'Donnell</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Fort Lauderdale</td>\n",
       "      <td>Florida</td>\n",
       "      <td>33311.0</td>\n",
       "      <td>South</td>\n",
       "      <td>OFF-ST-10000760</td>\n",
       "      <td>Eldon Fold 'N Roll Cart System</td>\n",
       "      <td>22.3680</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Supplies</th>\n",
       "      <td>139</td>\n",
       "      <td>CA-2017-145583</td>\n",
       "      <td>2017-10-13</td>\n",
       "      <td>2017-10-19</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>LC-16885</td>\n",
       "      <td>Lena Creighton</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Roseville</td>\n",
       "      <td>California</td>\n",
       "      <td>95661.0</td>\n",
       "      <td>West</td>\n",
       "      <td>OFF-SU-10001218</td>\n",
       "      <td>Fiskars Softgrip Scissors</td>\n",
       "      <td>65.8800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Technology</th>\n",
       "      <th>Accessories</th>\n",
       "      <td>27</td>\n",
       "      <td>CA-2017-121755</td>\n",
       "      <td>2017-01-16</td>\n",
       "      <td>2017-01-20</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>EH-13945</td>\n",
       "      <td>Eric Hoffmann</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90049.0</td>\n",
       "      <td>West</td>\n",
       "      <td>TEC-AC-10003027</td>\n",
       "      <td>Imation 8GB Mini TravelDrive USB 2.0 Flash Drive</td>\n",
       "      <td>90.5700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Copiers</th>\n",
       "      <td>336</td>\n",
       "      <td>CA-2016-137946</td>\n",
       "      <td>2016-09-01</td>\n",
       "      <td>2016-09-04</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>DB-13615</td>\n",
       "      <td>Doug Bickford</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90045.0</td>\n",
       "      <td>West</td>\n",
       "      <td>TEC-CO-10001449</td>\n",
       "      <td>Hewlett Packard LaserJet 3310 Copier</td>\n",
       "      <td>959.9840</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Machines</th>\n",
       "      <td>166</td>\n",
       "      <td>CA-2015-139892</td>\n",
       "      <td>2015-09-08</td>\n",
       "      <td>2015-09-12</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>BM-11140</td>\n",
       "      <td>Becky Martin</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>San Antonio</td>\n",
       "      <td>Texas</td>\n",
       "      <td>78207.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>TEC-MA-10000822</td>\n",
       "      <td>Lexmark MX611dhe Monochrome Laser Printer</td>\n",
       "      <td>8159.9520</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Phones</th>\n",
       "      <td>8</td>\n",
       "      <td>CA-2015-115812</td>\n",
       "      <td>2015-06-09</td>\n",
       "      <td>2015-06-14</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>BH-11710</td>\n",
       "      <td>Brosina Hoffman</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90032.0</td>\n",
       "      <td>West</td>\n",
       "      <td>TEC-PH-10002275</td>\n",
       "      <td>Mitel 5320 IP Phone VoIP phone</td>\n",
       "      <td>907.1520</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              row_id        order_id order_date  ship_date  \\\n",
       "category        sub_category                                                 \n",
       "Furniture       Bookcases          1  CA-2017-152156 2017-11-08 2017-11-11   \n",
       "                Chairs             2  CA-2017-152156 2017-11-08 2017-11-11   \n",
       "                Furnishings        6  CA-2015-115812 2015-06-09 2015-06-14   \n",
       "                Tables             4  US-2016-108966 2016-10-11 2016-10-18   \n",
       "Office Supplies Appliances        10  CA-2015-115812 2015-06-09 2015-06-14   \n",
       "                Art                7  CA-2015-115812 2015-06-09 2015-06-14   \n",
       "                Binders            9  CA-2015-115812 2015-06-09 2015-06-14   \n",
       "                Envelopes         31  US-2016-150630 2016-09-17 2016-09-21   \n",
       "                Fasteners         54  CA-2017-105816 2017-12-11 2017-12-17   \n",
       "                Labels             3  CA-2017-138688 2017-06-12 2017-06-16   \n",
       "                Paper             13  CA-2018-114412 2018-04-15 2018-04-20   \n",
       "                Storage            5  US-2016-108966 2016-10-11 2016-10-18   \n",
       "                Supplies         139  CA-2017-145583 2017-10-13 2017-10-19   \n",
       "Technology      Accessories       27  CA-2017-121755 2017-01-16 2017-01-20   \n",
       "                Copiers          336  CA-2016-137946 2016-09-01 2016-09-04   \n",
       "                Machines         166  CA-2015-139892 2015-09-08 2015-09-12   \n",
       "                Phones             8  CA-2015-115812 2015-06-09 2015-06-14   \n",
       "\n",
       "                                   ship_mode customer_id    customer_name  \\\n",
       "category        sub_category                                                \n",
       "Furniture       Bookcases       Second Class    CG-12520      Claire Gute   \n",
       "                Chairs          Second Class    CG-12520      Claire Gute   \n",
       "                Furnishings   Standard Class    BH-11710  Brosina Hoffman   \n",
       "                Tables        Standard Class    SO-20335   Sean O'Donnell   \n",
       "Office Supplies Appliances    Standard Class    BH-11710  Brosina Hoffman   \n",
       "                Art           Standard Class    BH-11710  Brosina Hoffman   \n",
       "                Binders       Standard Class    BH-11710  Brosina Hoffman   \n",
       "                Envelopes     Standard Class    TB-21520  Tracy Blumstein   \n",
       "                Fasteners     Standard Class    JM-15265   Janet Molinari   \n",
       "                Labels          Second Class    DV-13045  Darrin Van Huff   \n",
       "                Paper         Standard Class    AA-10480     Andrew Allen   \n",
       "                Storage       Standard Class    SO-20335   Sean O'Donnell   \n",
       "                Supplies      Standard Class    LC-16885   Lena Creighton   \n",
       "Technology      Accessories     Second Class    EH-13945    Eric Hoffmann   \n",
       "                Copiers         Second Class    DB-13615    Doug Bickford   \n",
       "                Machines      Standard Class    BM-11140     Becky Martin   \n",
       "                Phones        Standard Class    BH-11710  Brosina Hoffman   \n",
       "\n",
       "                                segment        country             city  \\\n",
       "category        sub_category                                              \n",
       "Furniture       Bookcases      Consumer  United States        Henderson   \n",
       "                Chairs         Consumer  United States        Henderson   \n",
       "                Furnishings    Consumer  United States      Los Angeles   \n",
       "                Tables         Consumer  United States  Fort Lauderdale   \n",
       "Office Supplies Appliances     Consumer  United States      Los Angeles   \n",
       "                Art            Consumer  United States      Los Angeles   \n",
       "                Binders        Consumer  United States      Los Angeles   \n",
       "                Envelopes      Consumer  United States     Philadelphia   \n",
       "                Fasteners     Corporate  United States    New York City   \n",
       "                Labels        Corporate  United States      Los Angeles   \n",
       "                Paper          Consumer  United States          Concord   \n",
       "                Storage        Consumer  United States  Fort Lauderdale   \n",
       "                Supplies       Consumer  United States        Roseville   \n",
       "Technology      Accessories    Consumer  United States      Los Angeles   \n",
       "                Copiers        Consumer  United States      Los Angeles   \n",
       "                Machines       Consumer  United States      San Antonio   \n",
       "                Phones         Consumer  United States      Los Angeles   \n",
       "\n",
       "                                       state  postal_code   region  \\\n",
       "category        sub_category                                         \n",
       "Furniture       Bookcases           Kentucky      42420.0    South   \n",
       "                Chairs              Kentucky      42420.0    South   \n",
       "                Furnishings       California      90032.0     West   \n",
       "                Tables               Florida      33311.0    South   \n",
       "Office Supplies Appliances        California      90032.0     West   \n",
       "                Art               California      90032.0     West   \n",
       "                Binders           California      90032.0     West   \n",
       "                Envelopes       Pennsylvania      19140.0     East   \n",
       "                Fasteners           New York      10024.0     East   \n",
       "                Labels            California      90036.0     West   \n",
       "                Paper         North Carolina      28027.0    South   \n",
       "                Storage              Florida      33311.0    South   \n",
       "                Supplies          California      95661.0     West   \n",
       "Technology      Accessories       California      90049.0     West   \n",
       "                Copiers           California      90045.0     West   \n",
       "                Machines               Texas      78207.0  Central   \n",
       "                Phones            California      90032.0     West   \n",
       "\n",
       "                                   product_id  \\\n",
       "category        sub_category                    \n",
       "Furniture       Bookcases     FUR-BO-10001798   \n",
       "                Chairs        FUR-CH-10000454   \n",
       "                Furnishings   FUR-FU-10001487   \n",
       "                Tables        FUR-TA-10000577   \n",
       "Office Supplies Appliances    OFF-AP-10002892   \n",
       "                Art           OFF-AR-10002833   \n",
       "                Binders       OFF-BI-10003910   \n",
       "                Envelopes     OFF-EN-10001509   \n",
       "                Fasteners     OFF-FA-10000304   \n",
       "                Labels        OFF-LA-10000240   \n",
       "                Paper         OFF-PA-10002365   \n",
       "                Storage       OFF-ST-10000760   \n",
       "                Supplies      OFF-SU-10001218   \n",
       "Technology      Accessories   TEC-AC-10003027   \n",
       "                Copiers       TEC-CO-10001449   \n",
       "                Machines      TEC-MA-10000822   \n",
       "                Phones        TEC-PH-10002275   \n",
       "\n",
       "                                                                   product_name  \\\n",
       "category        sub_category                                                      \n",
       "Furniture       Bookcases                     Bush Somerset Collection Bookcase   \n",
       "                Chairs        Hon Deluxe Fabric Upholstered Stacking Chairs,...   \n",
       "                Furnishings   Eldon Expressions Wood and Plastic Desk Access...   \n",
       "                Tables            Bretford CR4500 Series Slim Rectangular Table   \n",
       "Office Supplies Appliances                     Belkin F5C206VTEL 6 Outlet Surge   \n",
       "                Art                                                  Newell 322   \n",
       "                Binders       DXL Angle-View Binders with Locking Rings by S...   \n",
       "                Envelopes                             Poly String Tie Envelopes   \n",
       "                Fasteners                                    Advantus Push Pins   \n",
       "                Labels        Self-Adhesive Address Labels for Typewriters b...   \n",
       "                Paper                                                Xerox 1967   \n",
       "                Storage                          Eldon Fold 'N Roll Cart System   \n",
       "                Supplies                              Fiskars Softgrip Scissors   \n",
       "Technology      Accessories    Imation 8GB Mini TravelDrive USB 2.0 Flash Drive   \n",
       "                Copiers                    Hewlett Packard LaserJet 3310 Copier   \n",
       "                Machines              Lexmark MX611dhe Monochrome Laser Printer   \n",
       "                Phones                           Mitel 5320 IP Phone VoIP phone   \n",
       "\n",
       "                                  sales  \n",
       "category        sub_category             \n",
       "Furniture       Bookcases      261.9600  \n",
       "                Chairs         731.9400  \n",
       "                Furnishings     48.8600  \n",
       "                Tables         957.5775  \n",
       "Office Supplies Appliances     114.9000  \n",
       "                Art              7.2800  \n",
       "                Binders         18.5040  \n",
       "                Envelopes        3.2640  \n",
       "                Fasteners       15.2600  \n",
       "                Labels          14.6200  \n",
       "                Paper           15.5520  \n",
       "                Storage         22.3680  \n",
       "                Supplies        65.8800  \n",
       "Technology      Accessories     90.5700  \n",
       "                Copiers        959.9840  \n",
       "                Machines      8159.9520  \n",
       "                Phones         907.1520  "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df.first()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>order_id</th>\n",
       "      <th>order_date</th>\n",
       "      <th>ship_date</th>\n",
       "      <th>ship_mode</th>\n",
       "      <th>customer_id</th>\n",
       "      <th>customer_name</th>\n",
       "      <th>segment</th>\n",
       "      <th>country</th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "      <th>postal_code</th>\n",
       "      <th>region</th>\n",
       "      <th>product_id</th>\n",
       "      <th>product_name</th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>category</th>\n",
       "      <th>sub_category</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Furniture</th>\n",
       "      <th>Bookcases</th>\n",
       "      <td>9788</td>\n",
       "      <td>CA-2018-144491</td>\n",
       "      <td>2018-03-27</td>\n",
       "      <td>2018-04-01</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CJ-12010</td>\n",
       "      <td>Caroline Jumper</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Houston</td>\n",
       "      <td>Texas</td>\n",
       "      <td>77070.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>FUR-BO-10001811</td>\n",
       "      <td>Atlantic Metals Mobile 5-Shelf Bookcases, Cust...</td>\n",
       "      <td>1023.332</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chairs</th>\n",
       "      <td>9793</td>\n",
       "      <td>CA-2015-127166</td>\n",
       "      <td>2015-05-21</td>\n",
       "      <td>2015-05-23</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>KH-16360</td>\n",
       "      <td>Katherine Hughes</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Houston</td>\n",
       "      <td>Texas</td>\n",
       "      <td>77070.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>FUR-CH-10003396</td>\n",
       "      <td>Global Deluxe Steno Chair</td>\n",
       "      <td>107.772</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Furnishings</th>\n",
       "      <td>9785</td>\n",
       "      <td>CA-2016-149748</td>\n",
       "      <td>2016-05-31</td>\n",
       "      <td>2016-06-02</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>EM-13825</td>\n",
       "      <td>Elizabeth Moffitt</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Paterson</td>\n",
       "      <td>New Jersey</td>\n",
       "      <td>7501.0</td>\n",
       "      <td>East</td>\n",
       "      <td>FUR-FU-10001847</td>\n",
       "      <td>Eldon Image Series Black Desk Accessories</td>\n",
       "      <td>8.280</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tables</th>\n",
       "      <td>9757</td>\n",
       "      <td>CA-2018-113705</td>\n",
       "      <td>2018-03-27</td>\n",
       "      <td>2018-03-29</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>LC-16870</td>\n",
       "      <td>Lena Cacioppo</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Richmond</td>\n",
       "      <td>Virginia</td>\n",
       "      <td>23223.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-TA-10002533</td>\n",
       "      <td>BPI Conference Tables</td>\n",
       "      <td>292.100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"9\" valign=\"top\">Office Supplies</th>\n",
       "      <th>Appliances</th>\n",
       "      <td>9780</td>\n",
       "      <td>CA-2015-169019</td>\n",
       "      <td>2015-07-26</td>\n",
       "      <td>2015-07-30</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>LF-17185</td>\n",
       "      <td>Luke Foster</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>San Antonio</td>\n",
       "      <td>Texas</td>\n",
       "      <td>78207.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>OFF-AP-10003281</td>\n",
       "      <td>Acco 6 Outlet Guardian Standard Surge Suppressor</td>\n",
       "      <td>4.836</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Art</th>\n",
       "      <td>9797</td>\n",
       "      <td>CA-2016-128608</td>\n",
       "      <td>2016-01-12</td>\n",
       "      <td>2016-01-17</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CS-12490</td>\n",
       "      <td>Cindy Schnelling</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Toledo</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>43615.0</td>\n",
       "      <td>East</td>\n",
       "      <td>OFF-AR-10001374</td>\n",
       "      <td>BIC Brite Liner Highlighters, Chisel Tip</td>\n",
       "      <td>10.368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Binders</th>\n",
       "      <td>9796</td>\n",
       "      <td>CA-2017-125920</td>\n",
       "      <td>2017-05-21</td>\n",
       "      <td>2017-05-28</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>SH-19975</td>\n",
       "      <td>Sally Hughsby</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Chicago</td>\n",
       "      <td>Illinois</td>\n",
       "      <td>60610.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>OFF-BI-10003429</td>\n",
       "      <td>Cardinal HOLDit! Binder Insert Strips,Extra St...</td>\n",
       "      <td>3.798</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Envelopes</th>\n",
       "      <td>9792</td>\n",
       "      <td>CA-2015-127166</td>\n",
       "      <td>2015-05-21</td>\n",
       "      <td>2015-05-23</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>KH-16360</td>\n",
       "      <td>Katherine Hughes</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Houston</td>\n",
       "      <td>Texas</td>\n",
       "      <td>77070.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>OFF-EN-10003134</td>\n",
       "      <td>Staple envelope</td>\n",
       "      <td>56.064</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Fasteners</th>\n",
       "      <td>9702</td>\n",
       "      <td>CA-2017-105291</td>\n",
       "      <td>2017-10-30</td>\n",
       "      <td>2017-11-04</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>SP-20920</td>\n",
       "      <td>Susan Pistek</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>San Luis Obispo</td>\n",
       "      <td>California</td>\n",
       "      <td>93405.0</td>\n",
       "      <td>West</td>\n",
       "      <td>OFF-FA-10003059</td>\n",
       "      <td>Assorted Color Push Pins</td>\n",
       "      <td>3.620</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Labels</th>\n",
       "      <td>9754</td>\n",
       "      <td>CA-2018-113705</td>\n",
       "      <td>2018-03-27</td>\n",
       "      <td>2018-03-29</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>LC-16870</td>\n",
       "      <td>Lena Cacioppo</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Richmond</td>\n",
       "      <td>Virginia</td>\n",
       "      <td>23223.0</td>\n",
       "      <td>South</td>\n",
       "      <td>OFF-LA-10000476</td>\n",
       "      <td>Avery 05222 Permanent Self-Adhesive File Folde...</td>\n",
       "      <td>8.260</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Paper</th>\n",
       "      <td>9794</td>\n",
       "      <td>CA-2015-127166</td>\n",
       "      <td>2015-05-21</td>\n",
       "      <td>2015-05-23</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>KH-16360</td>\n",
       "      <td>Katherine Hughes</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Houston</td>\n",
       "      <td>Texas</td>\n",
       "      <td>77070.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>OFF-PA-10001560</td>\n",
       "      <td>Adams Telephone Message Books, 5 1/4” x 11”</td>\n",
       "      <td>4.832</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Storage</th>\n",
       "      <td>9784</td>\n",
       "      <td>CA-2016-149748</td>\n",
       "      <td>2016-05-31</td>\n",
       "      <td>2016-06-02</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>EM-13825</td>\n",
       "      <td>Elizabeth Moffitt</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Paterson</td>\n",
       "      <td>New Jersey</td>\n",
       "      <td>7501.0</td>\n",
       "      <td>East</td>\n",
       "      <td>OFF-ST-10004340</td>\n",
       "      <td>Fellowes Mobile File Cart, Black</td>\n",
       "      <td>62.180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Supplies</th>\n",
       "      <td>9764</td>\n",
       "      <td>CA-2015-121762</td>\n",
       "      <td>2015-02-14</td>\n",
       "      <td>2015-02-18</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>ML-17395</td>\n",
       "      <td>Marina Lichtenstein</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Seattle</td>\n",
       "      <td>Washington</td>\n",
       "      <td>98103.0</td>\n",
       "      <td>West</td>\n",
       "      <td>OFF-SU-10000157</td>\n",
       "      <td>Compact Automatic Electric Letter Opener</td>\n",
       "      <td>238.620</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Technology</th>\n",
       "      <th>Accessories</th>\n",
       "      <td>9800</td>\n",
       "      <td>CA-2016-128608</td>\n",
       "      <td>2016-01-12</td>\n",
       "      <td>2016-01-17</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CS-12490</td>\n",
       "      <td>Cindy Schnelling</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Toledo</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>43615.0</td>\n",
       "      <td>East</td>\n",
       "      <td>TEC-AC-10000487</td>\n",
       "      <td>SanDisk Cruzer 4 GB USB Flash Drive</td>\n",
       "      <td>10.384</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Copiers</th>\n",
       "      <td>9618</td>\n",
       "      <td>CA-2018-160633</td>\n",
       "      <td>2018-11-16</td>\n",
       "      <td>2018-11-21</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>BS-11380</td>\n",
       "      <td>Bill Stewart</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Bowling Green</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>43402.0</td>\n",
       "      <td>East</td>\n",
       "      <td>TEC-CO-10002095</td>\n",
       "      <td>Hewlett Packard 610 Color Digital Copier / Pri...</td>\n",
       "      <td>899.982</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Machines</th>\n",
       "      <td>9577</td>\n",
       "      <td>CA-2016-143147</td>\n",
       "      <td>2016-05-26</td>\n",
       "      <td>2016-05-28</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>PS-18760</td>\n",
       "      <td>Pamela Stobb</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>San Antonio</td>\n",
       "      <td>Texas</td>\n",
       "      <td>78207.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>TEC-MA-10004679</td>\n",
       "      <td>StarTech.com 10/100 VDSL2 Ethernet Extender Kit</td>\n",
       "      <td>399.540</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Phones</th>\n",
       "      <td>9799</td>\n",
       "      <td>CA-2016-128608</td>\n",
       "      <td>2016-01-12</td>\n",
       "      <td>2016-01-17</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CS-12490</td>\n",
       "      <td>Cindy Schnelling</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Toledo</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>43615.0</td>\n",
       "      <td>East</td>\n",
       "      <td>TEC-PH-10000912</td>\n",
       "      <td>Anker 24W Portable Micro USB Car Charger</td>\n",
       "      <td>26.376</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              row_id        order_id order_date  ship_date  \\\n",
       "category        sub_category                                                 \n",
       "Furniture       Bookcases       9788  CA-2018-144491 2018-03-27 2018-04-01   \n",
       "                Chairs          9793  CA-2015-127166 2015-05-21 2015-05-23   \n",
       "                Furnishings     9785  CA-2016-149748 2016-05-31 2016-06-02   \n",
       "                Tables          9757  CA-2018-113705 2018-03-27 2018-03-29   \n",
       "Office Supplies Appliances      9780  CA-2015-169019 2015-07-26 2015-07-30   \n",
       "                Art             9797  CA-2016-128608 2016-01-12 2016-01-17   \n",
       "                Binders         9796  CA-2017-125920 2017-05-21 2017-05-28   \n",
       "                Envelopes       9792  CA-2015-127166 2015-05-21 2015-05-23   \n",
       "                Fasteners       9702  CA-2017-105291 2017-10-30 2017-11-04   \n",
       "                Labels          9754  CA-2018-113705 2018-03-27 2018-03-29   \n",
       "                Paper           9794  CA-2015-127166 2015-05-21 2015-05-23   \n",
       "                Storage         9784  CA-2016-149748 2016-05-31 2016-06-02   \n",
       "                Supplies        9764  CA-2015-121762 2015-02-14 2015-02-18   \n",
       "Technology      Accessories     9800  CA-2016-128608 2016-01-12 2016-01-17   \n",
       "                Copiers         9618  CA-2018-160633 2018-11-16 2018-11-21   \n",
       "                Machines        9577  CA-2016-143147 2016-05-26 2016-05-28   \n",
       "                Phones          9799  CA-2016-128608 2016-01-12 2016-01-17   \n",
       "\n",
       "                                   ship_mode customer_id        customer_name  \\\n",
       "category        sub_category                                                    \n",
       "Furniture       Bookcases     Standard Class    CJ-12010      Caroline Jumper   \n",
       "                Chairs          Second Class    KH-16360     Katherine Hughes   \n",
       "                Furnishings     Second Class    EM-13825    Elizabeth Moffitt   \n",
       "                Tables          Second Class    LC-16870        Lena Cacioppo   \n",
       "Office Supplies Appliances    Standard Class    LF-17185          Luke Foster   \n",
       "                Art           Standard Class    CS-12490     Cindy Schnelling   \n",
       "                Binders       Standard Class    SH-19975        Sally Hughsby   \n",
       "                Envelopes       Second Class    KH-16360     Katherine Hughes   \n",
       "                Fasteners     Standard Class    SP-20920         Susan Pistek   \n",
       "                Labels          Second Class    LC-16870        Lena Cacioppo   \n",
       "                Paper           Second Class    KH-16360     Katherine Hughes   \n",
       "                Storage         Second Class    EM-13825    Elizabeth Moffitt   \n",
       "                Supplies      Standard Class    ML-17395  Marina Lichtenstein   \n",
       "Technology      Accessories   Standard Class    CS-12490     Cindy Schnelling   \n",
       "                Copiers       Standard Class    BS-11380         Bill Stewart   \n",
       "                Machines        Second Class    PS-18760         Pamela Stobb   \n",
       "                Phones        Standard Class    CS-12490     Cindy Schnelling   \n",
       "\n",
       "                                segment        country             city  \\\n",
       "category        sub_category                                              \n",
       "Furniture       Bookcases      Consumer  United States          Houston   \n",
       "                Chairs         Consumer  United States          Houston   \n",
       "                Furnishings   Corporate  United States         Paterson   \n",
       "                Tables         Consumer  United States         Richmond   \n",
       "Office Supplies Appliances     Consumer  United States      San Antonio   \n",
       "                Art           Corporate  United States           Toledo   \n",
       "                Binders       Corporate  United States          Chicago   \n",
       "                Envelopes      Consumer  United States          Houston   \n",
       "                Fasteners      Consumer  United States  San Luis Obispo   \n",
       "                Labels         Consumer  United States         Richmond   \n",
       "                Paper          Consumer  United States          Houston   \n",
       "                Storage       Corporate  United States         Paterson   \n",
       "                Supplies      Corporate  United States          Seattle   \n",
       "Technology      Accessories   Corporate  United States           Toledo   \n",
       "                Copiers       Corporate  United States    Bowling Green   \n",
       "                Machines       Consumer  United States      San Antonio   \n",
       "                Phones        Corporate  United States           Toledo   \n",
       "\n",
       "                                   state  postal_code   region  \\\n",
       "category        sub_category                                     \n",
       "Furniture       Bookcases          Texas      77070.0  Central   \n",
       "                Chairs             Texas      77070.0  Central   \n",
       "                Furnishings   New Jersey       7501.0     East   \n",
       "                Tables          Virginia      23223.0    South   \n",
       "Office Supplies Appliances         Texas      78207.0  Central   \n",
       "                Art                 Ohio      43615.0     East   \n",
       "                Binders         Illinois      60610.0  Central   \n",
       "                Envelopes          Texas      77070.0  Central   \n",
       "                Fasteners     California      93405.0     West   \n",
       "                Labels          Virginia      23223.0    South   \n",
       "                Paper              Texas      77070.0  Central   \n",
       "                Storage       New Jersey       7501.0     East   \n",
       "                Supplies      Washington      98103.0     West   \n",
       "Technology      Accessories         Ohio      43615.0     East   \n",
       "                Copiers             Ohio      43402.0     East   \n",
       "                Machines           Texas      78207.0  Central   \n",
       "                Phones              Ohio      43615.0     East   \n",
       "\n",
       "                                   product_id  \\\n",
       "category        sub_category                    \n",
       "Furniture       Bookcases     FUR-BO-10001811   \n",
       "                Chairs        FUR-CH-10003396   \n",
       "                Furnishings   FUR-FU-10001847   \n",
       "                Tables        FUR-TA-10002533   \n",
       "Office Supplies Appliances    OFF-AP-10003281   \n",
       "                Art           OFF-AR-10001374   \n",
       "                Binders       OFF-BI-10003429   \n",
       "                Envelopes     OFF-EN-10003134   \n",
       "                Fasteners     OFF-FA-10003059   \n",
       "                Labels        OFF-LA-10000476   \n",
       "                Paper         OFF-PA-10001560   \n",
       "                Storage       OFF-ST-10004340   \n",
       "                Supplies      OFF-SU-10000157   \n",
       "Technology      Accessories   TEC-AC-10000487   \n",
       "                Copiers       TEC-CO-10002095   \n",
       "                Machines      TEC-MA-10004679   \n",
       "                Phones        TEC-PH-10000912   \n",
       "\n",
       "                                                                   product_name  \\\n",
       "category        sub_category                                                      \n",
       "Furniture       Bookcases     Atlantic Metals Mobile 5-Shelf Bookcases, Cust...   \n",
       "                Chairs                                Global Deluxe Steno Chair   \n",
       "                Furnishings           Eldon Image Series Black Desk Accessories   \n",
       "                Tables                                    BPI Conference Tables   \n",
       "Office Supplies Appliances     Acco 6 Outlet Guardian Standard Surge Suppressor   \n",
       "                Art                    BIC Brite Liner Highlighters, Chisel Tip   \n",
       "                Binders       Cardinal HOLDit! Binder Insert Strips,Extra St...   \n",
       "                Envelopes                                       Staple envelope   \n",
       "                Fasteners                              Assorted Color Push Pins   \n",
       "                Labels        Avery 05222 Permanent Self-Adhesive File Folde...   \n",
       "                Paper               Adams Telephone Message Books, 5 1/4” x 11”   \n",
       "                Storage                        Fellowes Mobile File Cart, Black   \n",
       "                Supplies               Compact Automatic Electric Letter Opener   \n",
       "Technology      Accessories                 SanDisk Cruzer 4 GB USB Flash Drive   \n",
       "                Copiers       Hewlett Packard 610 Color Digital Copier / Pri...   \n",
       "                Machines        StarTech.com 10/100 VDSL2 Ethernet Extender Kit   \n",
       "                Phones                 Anker 24W Portable Micro USB Car Charger   \n",
       "\n",
       "                                 sales  \n",
       "category        sub_category            \n",
       "Furniture       Bookcases     1023.332  \n",
       "                Chairs         107.772  \n",
       "                Furnishings      8.280  \n",
       "                Tables         292.100  \n",
       "Office Supplies Appliances       4.836  \n",
       "                Art             10.368  \n",
       "                Binders          3.798  \n",
       "                Envelopes       56.064  \n",
       "                Fasteners        3.620  \n",
       "                Labels           8.260  \n",
       "                Paper            4.832  \n",
       "                Storage         62.180  \n",
       "                Supplies       238.620  \n",
       "Technology      Accessories     10.384  \n",
       "                Copiers        899.982  \n",
       "                Machines       399.540  \n",
       "                Phones          26.376  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df.last()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>order_id</th>\n",
       "      <th>order_date</th>\n",
       "      <th>ship_date</th>\n",
       "      <th>ship_mode</th>\n",
       "      <th>customer_id</th>\n",
       "      <th>customer_name</th>\n",
       "      <th>segment</th>\n",
       "      <th>country</th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "      <th>postal_code</th>\n",
       "      <th>region</th>\n",
       "      <th>product_id</th>\n",
       "      <th>product_name</th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>category</th>\n",
       "      <th>sub_category</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Furniture</th>\n",
       "      <th>Bookcases</th>\n",
       "      <td>413</td>\n",
       "      <td>CA-2018-117457</td>\n",
       "      <td>2018-12-08</td>\n",
       "      <td>2018-12-12</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>KH-16510</td>\n",
       "      <td>Keith Herrera</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>San Francisco</td>\n",
       "      <td>California</td>\n",
       "      <td>94110.0</td>\n",
       "      <td>West</td>\n",
       "      <td>FUR-BO-10001972</td>\n",
       "      <td>O'Sullivan 4-Shelf Bookcase in Odessa Pine</td>\n",
       "      <td>1336.829</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chairs</th>\n",
       "      <td>150</td>\n",
       "      <td>CA-2017-114489</td>\n",
       "      <td>2017-12-05</td>\n",
       "      <td>2017-12-09</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>JE-16165</td>\n",
       "      <td>Justin Ellison</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Franklin</td>\n",
       "      <td>Wisconsin</td>\n",
       "      <td>53132.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>FUR-CH-10000454</td>\n",
       "      <td>Hon Deluxe Fabric Upholstered Stacking Chairs,...</td>\n",
       "      <td>1951.840</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Furnishings</th>\n",
       "      <td>105</td>\n",
       "      <td>US-2016-156867</td>\n",
       "      <td>2016-11-13</td>\n",
       "      <td>2016-11-17</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>LC-16870</td>\n",
       "      <td>Lena Cacioppo</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Aurora</td>\n",
       "      <td>Colorado</td>\n",
       "      <td>80013.0</td>\n",
       "      <td>West</td>\n",
       "      <td>FUR-FU-10004006</td>\n",
       "      <td>Deflect-o DuraMat Lighweight, Studded, Beveled...</td>\n",
       "      <td>102.360</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tables</th>\n",
       "      <td>283</td>\n",
       "      <td>CA-2016-130890</td>\n",
       "      <td>2016-11-02</td>\n",
       "      <td>2016-11-06</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>JO-15280</td>\n",
       "      <td>Jas O'Carroll</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90004.0</td>\n",
       "      <td>West</td>\n",
       "      <td>FUR-TA-10002903</td>\n",
       "      <td>Bevis Round Bullnose 29\" High Table Top</td>\n",
       "      <td>1038.840</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"9\" valign=\"top\">Office Supplies</th>\n",
       "      <th>Appliances</th>\n",
       "      <td>203</td>\n",
       "      <td>CA-2015-133690</td>\n",
       "      <td>2015-08-03</td>\n",
       "      <td>2015-08-05</td>\n",
       "      <td>First Class</td>\n",
       "      <td>BS-11755</td>\n",
       "      <td>Bruce Stewart</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Denver</td>\n",
       "      <td>Colorado</td>\n",
       "      <td>80219.0</td>\n",
       "      <td>West</td>\n",
       "      <td>OFF-AP-10003622</td>\n",
       "      <td>Bravo II Megaboss 12-Amp Hard Body Upright, Re...</td>\n",
       "      <td>2.600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Art</th>\n",
       "      <td>112</td>\n",
       "      <td>CA-2017-128867</td>\n",
       "      <td>2017-11-03</td>\n",
       "      <td>2017-11-10</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CL-12565</td>\n",
       "      <td>Clay Ludtke</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Urbandale</td>\n",
       "      <td>Iowa</td>\n",
       "      <td>50322.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>OFF-AR-10000380</td>\n",
       "      <td>Hunt PowerHouse Electric Pencil Sharpener, Blue</td>\n",
       "      <td>75.960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Binders</th>\n",
       "      <td>64</td>\n",
       "      <td>CA-2016-135545</td>\n",
       "      <td>2016-11-24</td>\n",
       "      <td>2016-11-30</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>KM-16720</td>\n",
       "      <td>Kunst Miller</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90004.0</td>\n",
       "      <td>West</td>\n",
       "      <td>OFF-BI-10001078</td>\n",
       "      <td>Acco PRESSTEX Data Binder with Storage Hooks, ...</td>\n",
       "      <td>25.824</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Envelopes</th>\n",
       "      <td>270</td>\n",
       "      <td>US-2018-145366</td>\n",
       "      <td>2018-12-09</td>\n",
       "      <td>2018-12-13</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CA-12310</td>\n",
       "      <td>Christine Abelman</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Cincinnati</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>45231.0</td>\n",
       "      <td>East</td>\n",
       "      <td>OFF-EN-10004386</td>\n",
       "      <td>Recycled Interoffice Envelopes with String and...</td>\n",
       "      <td>57.576</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Fasteners</th>\n",
       "      <td>340</td>\n",
       "      <td>CA-2016-128167</td>\n",
       "      <td>2016-06-22</td>\n",
       "      <td>2016-06-26</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>KL-16645</td>\n",
       "      <td>Ken Lonsdale</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Layton</td>\n",
       "      <td>Utah</td>\n",
       "      <td>84041.0</td>\n",
       "      <td>West</td>\n",
       "      <td>OFF-FA-10000490</td>\n",
       "      <td>OIC Binder Clips, Mini, 1/4\" Capacity, Black</td>\n",
       "      <td>4.960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Labels</th>\n",
       "      <td>361</td>\n",
       "      <td>CA-2018-155698</td>\n",
       "      <td>2018-03-08</td>\n",
       "      <td>2018-03-11</td>\n",
       "      <td>First Class</td>\n",
       "      <td>VB-21745</td>\n",
       "      <td>Victoria Brennan</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Columbus</td>\n",
       "      <td>Georgia</td>\n",
       "      <td>31907.0</td>\n",
       "      <td>South</td>\n",
       "      <td>OFF-LA-10001158</td>\n",
       "      <td>Avery Address/Shipping Labels for Typewriters,...</td>\n",
       "      <td>20.700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Paper</th>\n",
       "      <td>103</td>\n",
       "      <td>CA-2017-129903</td>\n",
       "      <td>2017-12-01</td>\n",
       "      <td>2017-12-04</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>GZ-14470</td>\n",
       "      <td>Gary Zandusky</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Rochester</td>\n",
       "      <td>Minnesota</td>\n",
       "      <td>55901.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>OFF-PA-10004040</td>\n",
       "      <td>Universal Premium White Copier/Laser Paper (20...</td>\n",
       "      <td>23.920</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Storage</th>\n",
       "      <td>85</td>\n",
       "      <td>US-2018-119662</td>\n",
       "      <td>2018-11-13</td>\n",
       "      <td>2018-11-16</td>\n",
       "      <td>First Class</td>\n",
       "      <td>CS-12400</td>\n",
       "      <td>Christopher Schild</td>\n",
       "      <td>Home Office</td>\n",
       "      <td>United States</td>\n",
       "      <td>Chicago</td>\n",
       "      <td>Illinois</td>\n",
       "      <td>60623.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>OFF-ST-10003656</td>\n",
       "      <td>Safco Industrial Wire Shelving</td>\n",
       "      <td>230.376</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Supplies</th>\n",
       "      <td>599</td>\n",
       "      <td>CA-2017-120180</td>\n",
       "      <td>2017-07-14</td>\n",
       "      <td>2017-07-16</td>\n",
       "      <td>First Class</td>\n",
       "      <td>TP-21130</td>\n",
       "      <td>Theone Pippenger</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Philadelphia</td>\n",
       "      <td>Pennsylvania</td>\n",
       "      <td>19134.0</td>\n",
       "      <td>East</td>\n",
       "      <td>OFF-SU-10004115</td>\n",
       "      <td>Acme Stainless Steel Office Snips</td>\n",
       "      <td>11.632</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Technology</th>\n",
       "      <th>Accessories</th>\n",
       "      <td>162</td>\n",
       "      <td>CA-2016-119697</td>\n",
       "      <td>2016-12-28</td>\n",
       "      <td>2016-12-31</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>EM-13960</td>\n",
       "      <td>Eric Murdock</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Philadelphia</td>\n",
       "      <td>Pennsylvania</td>\n",
       "      <td>19134.0</td>\n",
       "      <td>East</td>\n",
       "      <td>TEC-AC-10003657</td>\n",
       "      <td>Lenovo 17-Key USB Numeric Keypad</td>\n",
       "      <td>54.384</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Copiers</th>\n",
       "      <td>1987</td>\n",
       "      <td>CA-2017-147417</td>\n",
       "      <td>2017-07-25</td>\n",
       "      <td>2017-07-27</td>\n",
       "      <td>First Class</td>\n",
       "      <td>CB-12415</td>\n",
       "      <td>Christy Brittain</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Columbus</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>43229.0</td>\n",
       "      <td>East</td>\n",
       "      <td>TEC-CO-10001449</td>\n",
       "      <td>Hewlett Packard LaserJet 3310 Copier</td>\n",
       "      <td>1439.976</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Machines</th>\n",
       "      <td>836</td>\n",
       "      <td>CA-2017-165316</td>\n",
       "      <td>2017-07-23</td>\n",
       "      <td>2017-07-27</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>JB-15400</td>\n",
       "      <td>Jennifer Braxton</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Tampa</td>\n",
       "      <td>Florida</td>\n",
       "      <td>33614.0</td>\n",
       "      <td>South</td>\n",
       "      <td>TEC-MA-10004002</td>\n",
       "      <td>Zebra GX420t Direct Thermal/Thermal Transfer P...</td>\n",
       "      <td>265.475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Phones</th>\n",
       "      <td>108</td>\n",
       "      <td>CA-2018-119004</td>\n",
       "      <td>2018-11-23</td>\n",
       "      <td>2018-11-28</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>JM-15250</td>\n",
       "      <td>Janet Martin</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Charlotte</td>\n",
       "      <td>North Carolina</td>\n",
       "      <td>28205.0</td>\n",
       "      <td>South</td>\n",
       "      <td>TEC-PH-10002844</td>\n",
       "      <td>Speck Products Candyshell Flip Case</td>\n",
       "      <td>27.992</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              row_id        order_id order_date  ship_date  \\\n",
       "category        sub_category                                                 \n",
       "Furniture       Bookcases        413  CA-2018-117457 2018-12-08 2018-12-12   \n",
       "                Chairs           150  CA-2017-114489 2017-12-05 2017-12-09   \n",
       "                Furnishings      105  US-2016-156867 2016-11-13 2016-11-17   \n",
       "                Tables           283  CA-2016-130890 2016-11-02 2016-11-06   \n",
       "Office Supplies Appliances       203  CA-2015-133690 2015-08-03 2015-08-05   \n",
       "                Art              112  CA-2017-128867 2017-11-03 2017-11-10   \n",
       "                Binders           64  CA-2016-135545 2016-11-24 2016-11-30   \n",
       "                Envelopes        270  US-2018-145366 2018-12-09 2018-12-13   \n",
       "                Fasteners        340  CA-2016-128167 2016-06-22 2016-06-26   \n",
       "                Labels           361  CA-2018-155698 2018-03-08 2018-03-11   \n",
       "                Paper            103  CA-2017-129903 2017-12-01 2017-12-04   \n",
       "                Storage           85  US-2018-119662 2018-11-13 2018-11-16   \n",
       "                Supplies         599  CA-2017-120180 2017-07-14 2017-07-16   \n",
       "Technology      Accessories      162  CA-2016-119697 2016-12-28 2016-12-31   \n",
       "                Copiers         1987  CA-2017-147417 2017-07-25 2017-07-27   \n",
       "                Machines         836  CA-2017-165316 2017-07-23 2017-07-27   \n",
       "                Phones           108  CA-2018-119004 2018-11-23 2018-11-28   \n",
       "\n",
       "                                   ship_mode customer_id       customer_name  \\\n",
       "category        sub_category                                                   \n",
       "Furniture       Bookcases     Standard Class    KH-16510       Keith Herrera   \n",
       "                Chairs        Standard Class    JE-16165      Justin Ellison   \n",
       "                Furnishings   Standard Class    LC-16870       Lena Cacioppo   \n",
       "                Tables        Standard Class    JO-15280       Jas O'Carroll   \n",
       "Office Supplies Appliances       First Class    BS-11755       Bruce Stewart   \n",
       "                Art           Standard Class    CL-12565         Clay Ludtke   \n",
       "                Binders       Standard Class    KM-16720        Kunst Miller   \n",
       "                Envelopes     Standard Class    CA-12310   Christine Abelman   \n",
       "                Fasteners       Second Class    KL-16645        Ken Lonsdale   \n",
       "                Labels           First Class    VB-21745    Victoria Brennan   \n",
       "                Paper           Second Class    GZ-14470       Gary Zandusky   \n",
       "                Storage          First Class    CS-12400  Christopher Schild   \n",
       "                Supplies         First Class    TP-21130    Theone Pippenger   \n",
       "Technology      Accessories     Second Class    EM-13960        Eric Murdock   \n",
       "                Copiers          First Class    CB-12415    Christy Brittain   \n",
       "                Machines      Standard Class    JB-15400    Jennifer Braxton   \n",
       "                Phones        Standard Class    JM-15250        Janet Martin   \n",
       "\n",
       "                                  segment        country           city  \\\n",
       "category        sub_category                                              \n",
       "Furniture       Bookcases        Consumer  United States  San Francisco   \n",
       "                Chairs          Corporate  United States       Franklin   \n",
       "                Furnishings      Consumer  United States         Aurora   \n",
       "                Tables           Consumer  United States    Los Angeles   \n",
       "Office Supplies Appliances       Consumer  United States         Denver   \n",
       "                Art              Consumer  United States      Urbandale   \n",
       "                Binders          Consumer  United States    Los Angeles   \n",
       "                Envelopes       Corporate  United States     Cincinnati   \n",
       "                Fasteners        Consumer  United States         Layton   \n",
       "                Labels          Corporate  United States       Columbus   \n",
       "                Paper            Consumer  United States      Rochester   \n",
       "                Storage       Home Office  United States        Chicago   \n",
       "                Supplies         Consumer  United States   Philadelphia   \n",
       "Technology      Accessories      Consumer  United States   Philadelphia   \n",
       "                Copiers          Consumer  United States       Columbus   \n",
       "                Machines        Corporate  United States          Tampa   \n",
       "                Phones           Consumer  United States      Charlotte   \n",
       "\n",
       "                                       state  postal_code   region  \\\n",
       "category        sub_category                                         \n",
       "Furniture       Bookcases         California      94110.0     West   \n",
       "                Chairs             Wisconsin      53132.0  Central   \n",
       "                Furnishings         Colorado      80013.0     West   \n",
       "                Tables            California      90004.0     West   \n",
       "Office Supplies Appliances          Colorado      80219.0     West   \n",
       "                Art                     Iowa      50322.0  Central   \n",
       "                Binders           California      90004.0     West   \n",
       "                Envelopes               Ohio      45231.0     East   \n",
       "                Fasteners               Utah      84041.0     West   \n",
       "                Labels               Georgia      31907.0    South   \n",
       "                Paper              Minnesota      55901.0  Central   \n",
       "                Storage             Illinois      60623.0  Central   \n",
       "                Supplies        Pennsylvania      19134.0     East   \n",
       "Technology      Accessories     Pennsylvania      19134.0     East   \n",
       "                Copiers                 Ohio      43229.0     East   \n",
       "                Machines             Florida      33614.0    South   \n",
       "                Phones        North Carolina      28205.0    South   \n",
       "\n",
       "                                   product_id  \\\n",
       "category        sub_category                    \n",
       "Furniture       Bookcases     FUR-BO-10001972   \n",
       "                Chairs        FUR-CH-10000454   \n",
       "                Furnishings   FUR-FU-10004006   \n",
       "                Tables        FUR-TA-10002903   \n",
       "Office Supplies Appliances    OFF-AP-10003622   \n",
       "                Art           OFF-AR-10000380   \n",
       "                Binders       OFF-BI-10001078   \n",
       "                Envelopes     OFF-EN-10004386   \n",
       "                Fasteners     OFF-FA-10000490   \n",
       "                Labels        OFF-LA-10001158   \n",
       "                Paper         OFF-PA-10004040   \n",
       "                Storage       OFF-ST-10003656   \n",
       "                Supplies      OFF-SU-10004115   \n",
       "Technology      Accessories   TEC-AC-10003657   \n",
       "                Copiers       TEC-CO-10001449   \n",
       "                Machines      TEC-MA-10004002   \n",
       "                Phones        TEC-PH-10002844   \n",
       "\n",
       "                                                                   product_name  \\\n",
       "category        sub_category                                                      \n",
       "Furniture       Bookcases            O'Sullivan 4-Shelf Bookcase in Odessa Pine   \n",
       "                Chairs        Hon Deluxe Fabric Upholstered Stacking Chairs,...   \n",
       "                Furnishings   Deflect-o DuraMat Lighweight, Studded, Beveled...   \n",
       "                Tables                  Bevis Round Bullnose 29\" High Table Top   \n",
       "Office Supplies Appliances    Bravo II Megaboss 12-Amp Hard Body Upright, Re...   \n",
       "                Art             Hunt PowerHouse Electric Pencil Sharpener, Blue   \n",
       "                Binders       Acco PRESSTEX Data Binder with Storage Hooks, ...   \n",
       "                Envelopes     Recycled Interoffice Envelopes with String and...   \n",
       "                Fasteners          OIC Binder Clips, Mini, 1/4\" Capacity, Black   \n",
       "                Labels        Avery Address/Shipping Labels for Typewriters,...   \n",
       "                Paper         Universal Premium White Copier/Laser Paper (20...   \n",
       "                Storage                          Safco Industrial Wire Shelving   \n",
       "                Supplies                      Acme Stainless Steel Office Snips   \n",
       "Technology      Accessories                    Lenovo 17-Key USB Numeric Keypad   \n",
       "                Copiers                    Hewlett Packard LaserJet 3310 Copier   \n",
       "                Machines      Zebra GX420t Direct Thermal/Thermal Transfer P...   \n",
       "                Phones                      Speck Products Candyshell Flip Case   \n",
       "\n",
       "                                 sales  \n",
       "category        sub_category            \n",
       "Furniture       Bookcases     1336.829  \n",
       "                Chairs        1951.840  \n",
       "                Furnishings    102.360  \n",
       "                Tables        1038.840  \n",
       "Office Supplies Appliances       2.600  \n",
       "                Art             75.960  \n",
       "                Binders         25.824  \n",
       "                Envelopes       57.576  \n",
       "                Fasteners        4.960  \n",
       "                Labels          20.700  \n",
       "                Paper           23.920  \n",
       "                Storage        230.376  \n",
       "                Supplies        11.632  \n",
       "Technology      Accessories     54.384  \n",
       "                Copiers       1439.976  \n",
       "                Machines       265.475  \n",
       "                Phones          27.992  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df.nth(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### split-apply-combine\n",
    "\n",
    "<img style=\"float: right;\" width=\"400\" height=\"400\" src=\"images/02_split_apply_combine.PNG\">\n",
    "\n",
    "Calculating a given statistic (e.g. mean age) for each category in a column (e.g. male/female in the Sex column) is a common pattern. The `groupby` method is used to support this type of operations. This fits in the more general split-apply-combine pattern:\n",
    "- **Split** the data into groups\n",
    "- **Apply** a function to each group independently\n",
    "- **Combine** the results into a data structure\n",
    "\n",
    "In the `apply` step, we might wish to do one of the following:\n",
    "- **Aggregation:** compute a summary statistic (or statistics) for each group. Some examples:  \n",
    "> Compute group sums or means.  \n",
    "> Compute group sizes / counts.\n",
    "\n",
    "- **Filtration:** discard some groups, according to a group-wise computation that evaluates to True or False. Some examples:\n",
    "> Discard data that belong to groups with only a few members.  \n",
    "> Filter out data based on the group sum or mean.\n",
    "\n",
    "- **Transformation:** perform some group-specific computations and return a like-indexed object. Some examples:\n",
    "> Standardize data (zscore) within a group.  \n",
    "> Filling NAs within groups with a value derived from each group."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Aggregation\n",
    "\n",
    "#### Built-in Aggregation Methods\n",
    "\n",
    "Many common aggregations are built-in to GroupBy objects as methods. Of the methods listed below, those with a * do not have a Cython-optimized implementation.\n",
    "\n",
    "\n",
    "| Method | Description |\n",
    "|:--------|:--------|\n",
    "| any() | Compute whether any of the values in the groups are truthy |\n",
    "| all() | Compute whether all of the values in the groups are truthy |\n",
    "| count()| Compute the number of non-NA values in the groups |\n",
    "| cov() * | Compute the covariance of the groups |\n",
    "| first() | Compute the first occurring value in each group |\n",
    "| idxmax() * | Compute the index of the maximum value in each group |\n",
    "| idxmin() * | Compute the index of the minimum value in each group |\n",
    "| last() | Compute the last occurring value in each group |\n",
    "| max() | Compute the maximum value in each group |\n",
    "| mean() | Compute the mean of each group |\n",
    "| median() | Compute the median of each group |\n",
    "| min() | Compute the minimum value in each group |\n",
    "| nunique() | Compute the number of unique values in each group |\n",
    "| prod() | Compute the product of the values in each group |\n",
    "| quantile() | Compute a given quantile of the values in each group |\n",
    "| sem() | Compute the standard error of the mean of the values in each group |\n",
    "| size() | Compute the number of values in each group |\n",
    "| skew() * | Compute the skew of the values in each group |\n",
    "| std() | Compute the standard deviation of the values in each group |\n",
    "| sum() | Compute the sum of the values in each group |\n",
    "| var() | Compute the variance of the values in each group |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "grouped_df = df.groupby('category')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "category\n",
       "Furniture          2078\n",
       "Office Supplies    5909\n",
       "Technology         1813\n",
       "Name: category, dtype: int64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df['category'].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "category\n",
       "Furniture          1.892\n",
       "Office Supplies    0.444\n",
       "Technology         0.990\n",
       "Name: sales, dtype: float64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df['sales'].min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "category\n",
       "Furniture           4416.174\n",
       "Office Supplies     9892.740\n",
       "Technology         22638.480\n",
       "Name: sales, dtype: float64"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df['sales'].max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "category\n",
       "Furniture          350.653790\n",
       "Office Supplies    119.381001\n",
       "Technology         456.401474\n",
       "Name: sales, dtype: float64"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df['sales'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "grouped_df = df.groupby(['category', 'sub_category'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "category         sub_category\n",
       "Furniture        Bookcases        226\n",
       "                 Chairs           607\n",
       "                 Furnishings      931\n",
       "                 Tables           314\n",
       "Office Supplies  Appliances       459\n",
       "                 Art              785\n",
       "                 Binders         1492\n",
       "                 Envelopes        248\n",
       "                 Fasteners        214\n",
       "                 Labels           357\n",
       "                 Paper           1338\n",
       "                 Storage          832\n",
       "                 Supplies         184\n",
       "Technology       Accessories      756\n",
       "                 Copiers           66\n",
       "                 Machines         115\n",
       "                 Phones           876\n",
       "Name: sub_category, dtype: int64"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df['sub_category'].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "category         sub_category\n",
       "Furniture        Bookcases        35.490\n",
       "                 Chairs           26.640\n",
       "                 Furnishings       1.892\n",
       "                 Tables           24.368\n",
       "Office Supplies  Appliances        0.444\n",
       "                 Art               1.344\n",
       "                 Binders           0.556\n",
       "                 Envelopes         1.632\n",
       "                 Fasteners         1.240\n",
       "                 Labels            2.088\n",
       "                 Paper             3.380\n",
       "                 Storage           4.464\n",
       "                 Supplies          1.744\n",
       "Technology       Accessories       0.990\n",
       "                 Copiers         299.990\n",
       "                 Machines         11.560\n",
       "                 Phones            2.970\n",
       "Name: sales, dtype: float64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df['sales'].min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "category         sub_category\n",
       "Furniture        Bookcases        4404.900\n",
       "                 Chairs           4416.174\n",
       "                 Furnishings      1336.440\n",
       "                 Tables           4297.644\n",
       "Office Supplies  Appliances       2625.120\n",
       "                 Art              1113.024\n",
       "                 Binders          9892.740\n",
       "                 Envelopes         604.656\n",
       "                 Fasteners          93.360\n",
       "                 Labels            786.480\n",
       "                 Paper             733.950\n",
       "                 Storage          2934.330\n",
       "                 Supplies         8187.650\n",
       "Technology       Accessories      3347.370\n",
       "                 Copiers         17499.950\n",
       "                 Machines        22638.480\n",
       "                 Phones           4548.810\n",
       "Name: sales, dtype: float64"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df['sales'].max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "category         sub_category\n",
       "Furniture        Bookcases        503.598224\n",
       "                 Chairs           531.833165\n",
       "                 Furnishings       95.823865\n",
       "                 Tables           645.893720\n",
       "Office Supplies  Appliances       227.926804\n",
       "                 Art               34.019631\n",
       "                 Binders          134.067550\n",
       "                 Envelopes         65.032444\n",
       "                 Fasteners         14.027850\n",
       "                 Labels            34.587468\n",
       "                 Paper             57.420257\n",
       "                 Storage          263.633885\n",
       "                 Supplies         252.284283\n",
       "Technology       Accessories      217.178175\n",
       "                 Copiers         2215.880212\n",
       "                 Machines        1645.553313\n",
       "                 Phones           374.180877\n",
       "Name: sales, dtype: float64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df['sales'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "category         sub_category\n",
       "Furniture        Bookcases       9741\n",
       "                 Chairs          7243\n",
       "                 Furnishings     7387\n",
       "                 Tables          9639\n",
       "Office Supplies  Appliances      7579\n",
       "                 Art               67\n",
       "                 Binders         9039\n",
       "                 Envelopes       2516\n",
       "                 Fasteners       8006\n",
       "                 Labels          1621\n",
       "                 Paper           3262\n",
       "                 Storage         3070\n",
       "                 Supplies        2505\n",
       "Technology       Accessories      251\n",
       "                 Copiers         6826\n",
       "                 Machines        2697\n",
       "                 Phones          2492\n",
       "Name: sales, dtype: int64"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df['sales'].idxmax()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "row_id                          2493\n",
       "order_id              CA-2015-144624\n",
       "order_date       2015-11-19 00:00:00\n",
       "ship_date        2015-11-23 00:00:00\n",
       "ship_mode             Standard Class\n",
       "customer_id                 JM-15865\n",
       "customer_name            John Murray\n",
       "segment                     Consumer\n",
       "country                United States\n",
       "city                       Jamestown\n",
       "state                       New York\n",
       "postal_code                  14701.0\n",
       "region                          East\n",
       "product_id           TEC-PH-10002885\n",
       "category                  Technology\n",
       "sub_category                  Phones\n",
       "product_name          Apple iPhone 5\n",
       "sales                        4548.81\n",
       "Name: 2492, dtype: object"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[2492]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "row_id                                                        2698\n",
       "order_id                                            CA-2015-145317\n",
       "order_date                                     2015-03-18 00:00:00\n",
       "ship_date                                      2015-03-23 00:00:00\n",
       "ship_mode                                           Standard Class\n",
       "customer_id                                               SM-20320\n",
       "customer_name                                          Sean Miller\n",
       "segment                                                Home Office\n",
       "country                                              United States\n",
       "city                                                  Jacksonville\n",
       "state                                                      Florida\n",
       "postal_code                                                32216.0\n",
       "region                                                       South\n",
       "product_id                                         TEC-MA-10002412\n",
       "category                                                Technology\n",
       "sub_category                                              Machines\n",
       "product_name     Cisco TelePresence System EX90 Videoconferenci...\n",
       "sales                                                     22638.48\n",
       "Name: 2697, dtype: object"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[2697]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Aggregation with User-Defined Functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>min</th>\n",
       "      <th>mean</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>category</th>\n",
       "      <th>sub_category</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Furniture</th>\n",
       "      <th>Bookcases</th>\n",
       "      <td>35.490</td>\n",
       "      <td>503.598224</td>\n",
       "      <td>4404.900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chairs</th>\n",
       "      <td>26.640</td>\n",
       "      <td>531.833165</td>\n",
       "      <td>4416.174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Furnishings</th>\n",
       "      <td>1.892</td>\n",
       "      <td>95.823865</td>\n",
       "      <td>1336.440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tables</th>\n",
       "      <td>24.368</td>\n",
       "      <td>645.893720</td>\n",
       "      <td>4297.644</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"9\" valign=\"top\">Office Supplies</th>\n",
       "      <th>Appliances</th>\n",
       "      <td>0.444</td>\n",
       "      <td>227.926804</td>\n",
       "      <td>2625.120</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Art</th>\n",
       "      <td>1.344</td>\n",
       "      <td>34.019631</td>\n",
       "      <td>1113.024</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Binders</th>\n",
       "      <td>0.556</td>\n",
       "      <td>134.067550</td>\n",
       "      <td>9892.740</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Envelopes</th>\n",
       "      <td>1.632</td>\n",
       "      <td>65.032444</td>\n",
       "      <td>604.656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Fasteners</th>\n",
       "      <td>1.240</td>\n",
       "      <td>14.027850</td>\n",
       "      <td>93.360</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Labels</th>\n",
       "      <td>2.088</td>\n",
       "      <td>34.587468</td>\n",
       "      <td>786.480</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Paper</th>\n",
       "      <td>3.380</td>\n",
       "      <td>57.420257</td>\n",
       "      <td>733.950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Storage</th>\n",
       "      <td>4.464</td>\n",
       "      <td>263.633885</td>\n",
       "      <td>2934.330</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Supplies</th>\n",
       "      <td>1.744</td>\n",
       "      <td>252.284283</td>\n",
       "      <td>8187.650</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Technology</th>\n",
       "      <th>Accessories</th>\n",
       "      <td>0.990</td>\n",
       "      <td>217.178175</td>\n",
       "      <td>3347.370</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Copiers</th>\n",
       "      <td>299.990</td>\n",
       "      <td>2215.880212</td>\n",
       "      <td>17499.950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Machines</th>\n",
       "      <td>11.560</td>\n",
       "      <td>1645.553313</td>\n",
       "      <td>22638.480</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Phones</th>\n",
       "      <td>2.970</td>\n",
       "      <td>374.180877</td>\n",
       "      <td>4548.810</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  min         mean        max\n",
       "category        sub_category                                 \n",
       "Furniture       Bookcases      35.490   503.598224   4404.900\n",
       "                Chairs         26.640   531.833165   4416.174\n",
       "                Furnishings     1.892    95.823865   1336.440\n",
       "                Tables         24.368   645.893720   4297.644\n",
       "Office Supplies Appliances      0.444   227.926804   2625.120\n",
       "                Art             1.344    34.019631   1113.024\n",
       "                Binders         0.556   134.067550   9892.740\n",
       "                Envelopes       1.632    65.032444    604.656\n",
       "                Fasteners       1.240    14.027850     93.360\n",
       "                Labels          2.088    34.587468    786.480\n",
       "                Paper           3.380    57.420257    733.950\n",
       "                Storage         4.464   263.633885   2934.330\n",
       "                Supplies        1.744   252.284283   8187.650\n",
       "Technology      Accessories     0.990   217.178175   3347.370\n",
       "                Copiers       299.990  2215.880212  17499.950\n",
       "                Machines       11.560  1645.553313  22638.480\n",
       "                Phones          2.970   374.180877   4548.810"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df['sales'].agg([\"min\", \"mean\", \"max\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "category         sub_category\n",
       "Furniture        Bookcases        35.490\n",
       "                 Chairs           26.640\n",
       "                 Furnishings       1.892\n",
       "                 Tables           24.368\n",
       "Office Supplies  Appliances        0.444\n",
       "                 Art               1.344\n",
       "                 Binders           0.556\n",
       "                 Envelopes         1.632\n",
       "                 Fasteners         1.240\n",
       "                 Labels            2.088\n",
       "                 Paper             3.380\n",
       "                 Storage           4.464\n",
       "                 Supplies          1.744\n",
       "Technology       Accessories       0.990\n",
       "                 Copiers         299.990\n",
       "                 Machines         11.560\n",
       "                 Phones            2.970\n",
       "Name: sales, dtype: float64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df['sales'].agg(lambda values : min(values))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Applying different aggregation functions to DataFrame columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">order_date</th>\n",
       "      <th colspan=\"2\" halign=\"left\">sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>min</th>\n",
       "      <th>max</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>category</th>\n",
       "      <th>sub_category</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Furniture</th>\n",
       "      <th>Bookcases</th>\n",
       "      <td>2015-01-13</td>\n",
       "      <td>2018-12-30</td>\n",
       "      <td>503.598224</td>\n",
       "      <td>641.419280</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chairs</th>\n",
       "      <td>2015-01-06</td>\n",
       "      <td>2018-12-29</td>\n",
       "      <td>531.833165</td>\n",
       "      <td>551.180296</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Furnishings</th>\n",
       "      <td>2015-01-07</td>\n",
       "      <td>2018-12-29</td>\n",
       "      <td>95.823865</td>\n",
       "      <td>148.421490</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tables</th>\n",
       "      <td>2015-01-27</td>\n",
       "      <td>2018-12-25</td>\n",
       "      <td>645.893720</td>\n",
       "      <td>598.584981</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"9\" valign=\"top\">Office Supplies</th>\n",
       "      <th>Appliances</th>\n",
       "      <td>2015-01-18</td>\n",
       "      <td>2018-12-30</td>\n",
       "      <td>227.926804</td>\n",
       "      <td>378.006735</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Art</th>\n",
       "      <td>2015-01-05</td>\n",
       "      <td>2018-12-29</td>\n",
       "      <td>34.019631</td>\n",
       "      <td>60.301752</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Binders</th>\n",
       "      <td>2015-01-04</td>\n",
       "      <td>2018-12-30</td>\n",
       "      <td>134.067550</td>\n",
       "      <td>568.099970</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Envelopes</th>\n",
       "      <td>2015-01-13</td>\n",
       "      <td>2018-12-23</td>\n",
       "      <td>65.032444</td>\n",
       "      <td>85.170691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Fasteners</th>\n",
       "      <td>2015-01-06</td>\n",
       "      <td>2018-12-30</td>\n",
       "      <td>14.027850</td>\n",
       "      <td>12.466864</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Labels</th>\n",
       "      <td>2015-01-04</td>\n",
       "      <td>2018-12-28</td>\n",
       "      <td>34.587468</td>\n",
       "      <td>74.802711</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Paper</th>\n",
       "      <td>2015-01-03</td>\n",
       "      <td>2018-12-29</td>\n",
       "      <td>57.420257</td>\n",
       "      <td>78.492285</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Storage</th>\n",
       "      <td>2015-01-04</td>\n",
       "      <td>2018-12-28</td>\n",
       "      <td>263.633885</td>\n",
       "      <td>354.907482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Supplies</th>\n",
       "      <td>2015-02-14</td>\n",
       "      <td>2018-12-25</td>\n",
       "      <td>252.284283</td>\n",
       "      <td>938.087746</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Technology</th>\n",
       "      <th>Accessories</th>\n",
       "      <td>2015-01-09</td>\n",
       "      <td>2018-12-25</td>\n",
       "      <td>217.178175</td>\n",
       "      <td>337.723800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Copiers</th>\n",
       "      <td>2015-05-02</td>\n",
       "      <td>2018-12-24</td>\n",
       "      <td>2215.880212</td>\n",
       "      <td>3216.185499</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Machines</th>\n",
       "      <td>2015-03-14</td>\n",
       "      <td>2018-12-25</td>\n",
       "      <td>1645.553313</td>\n",
       "      <td>2765.102088</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Phones</th>\n",
       "      <td>2015-01-06</td>\n",
       "      <td>2018-12-30</td>\n",
       "      <td>374.180877</td>\n",
       "      <td>494.390228</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             order_date                   sales             \n",
       "                                    min        max         mean          std\n",
       "category        sub_category                                                \n",
       "Furniture       Bookcases    2015-01-13 2018-12-30   503.598224   641.419280\n",
       "                Chairs       2015-01-06 2018-12-29   531.833165   551.180296\n",
       "                Furnishings  2015-01-07 2018-12-29    95.823865   148.421490\n",
       "                Tables       2015-01-27 2018-12-25   645.893720   598.584981\n",
       "Office Supplies Appliances   2015-01-18 2018-12-30   227.926804   378.006735\n",
       "                Art          2015-01-05 2018-12-29    34.019631    60.301752\n",
       "                Binders      2015-01-04 2018-12-30   134.067550   568.099970\n",
       "                Envelopes    2015-01-13 2018-12-23    65.032444    85.170691\n",
       "                Fasteners    2015-01-06 2018-12-30    14.027850    12.466864\n",
       "                Labels       2015-01-04 2018-12-28    34.587468    74.802711\n",
       "                Paper        2015-01-03 2018-12-29    57.420257    78.492285\n",
       "                Storage      2015-01-04 2018-12-28   263.633885   354.907482\n",
       "                Supplies     2015-02-14 2018-12-25   252.284283   938.087746\n",
       "Technology      Accessories  2015-01-09 2018-12-25   217.178175   337.723800\n",
       "                Copiers      2015-05-02 2018-12-24  2215.880212  3216.185499\n",
       "                Machines     2015-03-14 2018-12-25  1645.553313  2765.102088\n",
       "                Phones       2015-01-06 2018-12-30   374.180877   494.390228"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df.agg({'order_date' : ['min', 'max'], 'sales': ['mean', 'std']})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Filteration\n",
    "\n",
    "A filtration is a GroupBy operation the subsets the original grouping object. It may either filter out entire groups, part of groups, or both. Filtrations return a filtered version of the calling object, including the grouping columns when provided. In the following example, `class` is included in the result.  \n",
    "\n",
    "#### Built-in Filteration\n",
    " | Method | Description | \n",
    " |:------|:------|\n",
    " | head() | Select the top row(s) of each group | \n",
    " | nth() | Select the nth row(s) of each group | \n",
    " | tail() | Select the bottom row(s) of each group | "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Filteration with User-Defined Functions\n",
    "\n",
    "The `filter` method takes a User-Defined Function (UDF) that, when applied to an entire group, returns either `True` or `False`. The result of the `filter` method is then the subset of groups for which the UDF returned `True`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "grouped_df = df.groupby('category')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "category\n",
       "Furniture          2078\n",
       "Office Supplies    5909\n",
       "Technology         1813\n",
       "Name: category, dtype: int64"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df['category'].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "category\n",
       "Furniture          350.653790\n",
       "Office Supplies    119.381001\n",
       "Technology         456.401474\n",
       "Name: sales, dtype: float64"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df['sales'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>order_id</th>\n",
       "      <th>order_date</th>\n",
       "      <th>ship_date</th>\n",
       "      <th>ship_mode</th>\n",
       "      <th>customer_id</th>\n",
       "      <th>customer_name</th>\n",
       "      <th>segment</th>\n",
       "      <th>country</th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "      <th>postal_code</th>\n",
       "      <th>region</th>\n",
       "      <th>product_id</th>\n",
       "      <th>category</th>\n",
       "      <th>sub_category</th>\n",
       "      <th>product_name</th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>CA-2017-152156</td>\n",
       "      <td>2017-11-08</td>\n",
       "      <td>2017-11-11</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>CG-12520</td>\n",
       "      <td>Claire Gute</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Henderson</td>\n",
       "      <td>Kentucky</td>\n",
       "      <td>42420.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-BO-10001798</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Bookcases</td>\n",
       "      <td>Bush Somerset Collection Bookcase</td>\n",
       "      <td>261.9600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>CA-2017-152156</td>\n",
       "      <td>2017-11-08</td>\n",
       "      <td>2017-11-11</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>CG-12520</td>\n",
       "      <td>Claire Gute</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Henderson</td>\n",
       "      <td>Kentucky</td>\n",
       "      <td>42420.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-CH-10000454</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Chairs</td>\n",
       "      <td>Hon Deluxe Fabric Upholstered Stacking Chairs,...</td>\n",
       "      <td>731.9400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>US-2016-108966</td>\n",
       "      <td>2016-10-11</td>\n",
       "      <td>2016-10-18</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>SO-20335</td>\n",
       "      <td>Sean O'Donnell</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Fort Lauderdale</td>\n",
       "      <td>Florida</td>\n",
       "      <td>33311.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-TA-10000577</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Tables</td>\n",
       "      <td>Bretford CR4500 Series Slim Rectangular Table</td>\n",
       "      <td>957.5775</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>CA-2015-115812</td>\n",
       "      <td>2015-06-09</td>\n",
       "      <td>2015-06-14</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>BH-11710</td>\n",
       "      <td>Brosina Hoffman</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90032.0</td>\n",
       "      <td>West</td>\n",
       "      <td>FUR-FU-10001487</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Furnishings</td>\n",
       "      <td>Eldon Expressions Wood and Plastic Desk Access...</td>\n",
       "      <td>48.8600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>CA-2015-115812</td>\n",
       "      <td>2015-06-09</td>\n",
       "      <td>2015-06-14</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>BH-11710</td>\n",
       "      <td>Brosina Hoffman</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90032.0</td>\n",
       "      <td>West</td>\n",
       "      <td>TEC-PH-10002275</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>Mitel 5320 IP Phone VoIP phone</td>\n",
       "      <td>907.1520</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9790</th>\n",
       "      <td>9791</td>\n",
       "      <td>CA-2018-144491</td>\n",
       "      <td>2018-03-27</td>\n",
       "      <td>2018-04-01</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CJ-12010</td>\n",
       "      <td>Caroline Jumper</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Houston</td>\n",
       "      <td>Texas</td>\n",
       "      <td>77070.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>FUR-CH-10001714</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Chairs</td>\n",
       "      <td>Global Leather &amp; Oak Executive Chair, Burgundy</td>\n",
       "      <td>211.2460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9792</th>\n",
       "      <td>9793</td>\n",
       "      <td>CA-2015-127166</td>\n",
       "      <td>2015-05-21</td>\n",
       "      <td>2015-05-23</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>KH-16360</td>\n",
       "      <td>Katherine Hughes</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Houston</td>\n",
       "      <td>Texas</td>\n",
       "      <td>77070.0</td>\n",
       "      <td>Central</td>\n",
       "      <td>FUR-CH-10003396</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Chairs</td>\n",
       "      <td>Global Deluxe Steno Chair</td>\n",
       "      <td>107.7720</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9797</th>\n",
       "      <td>9798</td>\n",
       "      <td>CA-2016-128608</td>\n",
       "      <td>2016-01-12</td>\n",
       "      <td>2016-01-17</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CS-12490</td>\n",
       "      <td>Cindy Schnelling</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Toledo</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>43615.0</td>\n",
       "      <td>East</td>\n",
       "      <td>TEC-PH-10004977</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>GE 30524EE4</td>\n",
       "      <td>235.1880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9798</th>\n",
       "      <td>9799</td>\n",
       "      <td>CA-2016-128608</td>\n",
       "      <td>2016-01-12</td>\n",
       "      <td>2016-01-17</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CS-12490</td>\n",
       "      <td>Cindy Schnelling</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Toledo</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>43615.0</td>\n",
       "      <td>East</td>\n",
       "      <td>TEC-PH-10000912</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Phones</td>\n",
       "      <td>Anker 24W Portable Micro USB Car Charger</td>\n",
       "      <td>26.3760</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9799</th>\n",
       "      <td>9800</td>\n",
       "      <td>CA-2016-128608</td>\n",
       "      <td>2016-01-12</td>\n",
       "      <td>2016-01-17</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>CS-12490</td>\n",
       "      <td>Cindy Schnelling</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Toledo</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>43615.0</td>\n",
       "      <td>East</td>\n",
       "      <td>TEC-AC-10000487</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Accessories</td>\n",
       "      <td>SanDisk Cruzer 4 GB USB Flash Drive</td>\n",
       "      <td>10.3840</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3891 rows × 18 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      row_id        order_id order_date  ship_date       ship_mode  \\\n",
       "0          1  CA-2017-152156 2017-11-08 2017-11-11    Second Class   \n",
       "1          2  CA-2017-152156 2017-11-08 2017-11-11    Second Class   \n",
       "3          4  US-2016-108966 2016-10-11 2016-10-18  Standard Class   \n",
       "5          6  CA-2015-115812 2015-06-09 2015-06-14  Standard Class   \n",
       "7          8  CA-2015-115812 2015-06-09 2015-06-14  Standard Class   \n",
       "...      ...             ...        ...        ...             ...   \n",
       "9790    9791  CA-2018-144491 2018-03-27 2018-04-01  Standard Class   \n",
       "9792    9793  CA-2015-127166 2015-05-21 2015-05-23    Second Class   \n",
       "9797    9798  CA-2016-128608 2016-01-12 2016-01-17  Standard Class   \n",
       "9798    9799  CA-2016-128608 2016-01-12 2016-01-17  Standard Class   \n",
       "9799    9800  CA-2016-128608 2016-01-12 2016-01-17  Standard Class   \n",
       "\n",
       "     customer_id     customer_name    segment        country             city  \\\n",
       "0       CG-12520       Claire Gute   Consumer  United States        Henderson   \n",
       "1       CG-12520       Claire Gute   Consumer  United States        Henderson   \n",
       "3       SO-20335    Sean O'Donnell   Consumer  United States  Fort Lauderdale   \n",
       "5       BH-11710   Brosina Hoffman   Consumer  United States      Los Angeles   \n",
       "7       BH-11710   Brosina Hoffman   Consumer  United States      Los Angeles   \n",
       "...          ...               ...        ...            ...              ...   \n",
       "9790    CJ-12010   Caroline Jumper   Consumer  United States          Houston   \n",
       "9792    KH-16360  Katherine Hughes   Consumer  United States          Houston   \n",
       "9797    CS-12490  Cindy Schnelling  Corporate  United States           Toledo   \n",
       "9798    CS-12490  Cindy Schnelling  Corporate  United States           Toledo   \n",
       "9799    CS-12490  Cindy Schnelling  Corporate  United States           Toledo   \n",
       "\n",
       "           state  postal_code   region       product_id    category  \\\n",
       "0       Kentucky      42420.0    South  FUR-BO-10001798   Furniture   \n",
       "1       Kentucky      42420.0    South  FUR-CH-10000454   Furniture   \n",
       "3        Florida      33311.0    South  FUR-TA-10000577   Furniture   \n",
       "5     California      90032.0     West  FUR-FU-10001487   Furniture   \n",
       "7     California      90032.0     West  TEC-PH-10002275  Technology   \n",
       "...          ...          ...      ...              ...         ...   \n",
       "9790       Texas      77070.0  Central  FUR-CH-10001714   Furniture   \n",
       "9792       Texas      77070.0  Central  FUR-CH-10003396   Furniture   \n",
       "9797        Ohio      43615.0     East  TEC-PH-10004977  Technology   \n",
       "9798        Ohio      43615.0     East  TEC-PH-10000912  Technology   \n",
       "9799        Ohio      43615.0     East  TEC-AC-10000487  Technology   \n",
       "\n",
       "     sub_category                                       product_name     sales  \n",
       "0       Bookcases                  Bush Somerset Collection Bookcase  261.9600  \n",
       "1          Chairs  Hon Deluxe Fabric Upholstered Stacking Chairs,...  731.9400  \n",
       "3          Tables      Bretford CR4500 Series Slim Rectangular Table  957.5775  \n",
       "5     Furnishings  Eldon Expressions Wood and Plastic Desk Access...   48.8600  \n",
       "7          Phones                     Mitel 5320 IP Phone VoIP phone  907.1520  \n",
       "...           ...                                                ...       ...  \n",
       "9790       Chairs     Global Leather & Oak Executive Chair, Burgundy  211.2460  \n",
       "9792       Chairs                          Global Deluxe Steno Chair  107.7720  \n",
       "9797       Phones                                        GE 30524EE4  235.1880  \n",
       "9798       Phones           Anker 24W Portable Micro USB Car Charger   26.3760  \n",
       "9799  Accessories                SanDisk Cruzer 4 GB USB Flash Drive   10.3840  \n",
       "\n",
       "[3891 rows x 18 columns]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df.filter(lambda group: group['sales'].mean() > 200)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Transformation\n",
    "\n",
    "Unlike aggregations, the groupings that are used to split the original object are not included in the result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>order_id</th>\n",
       "      <th>order_date</th>\n",
       "      <th>ship_date</th>\n",
       "      <th>ship_mode</th>\n",
       "      <th>customer_id</th>\n",
       "      <th>customer_name</th>\n",
       "      <th>segment</th>\n",
       "      <th>country</th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "      <th>postal_code</th>\n",
       "      <th>region</th>\n",
       "      <th>product_id</th>\n",
       "      <th>category</th>\n",
       "      <th>sub_category</th>\n",
       "      <th>product_name</th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>CA-2017-152156</td>\n",
       "      <td>2017-11-08</td>\n",
       "      <td>2017-11-11</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>CG-12520</td>\n",
       "      <td>Claire Gute</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Henderson</td>\n",
       "      <td>Kentucky</td>\n",
       "      <td>42420.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-BO-10001798</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Bookcases</td>\n",
       "      <td>Bush Somerset Collection Bookcase</td>\n",
       "      <td>261.9600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>CA-2017-152156</td>\n",
       "      <td>2017-11-08</td>\n",
       "      <td>2017-11-11</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>CG-12520</td>\n",
       "      <td>Claire Gute</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Henderson</td>\n",
       "      <td>Kentucky</td>\n",
       "      <td>42420.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-CH-10000454</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Chairs</td>\n",
       "      <td>Hon Deluxe Fabric Upholstered Stacking Chairs,...</td>\n",
       "      <td>731.9400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>CA-2017-138688</td>\n",
       "      <td>2017-06-12</td>\n",
       "      <td>2017-06-16</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>DV-13045</td>\n",
       "      <td>Darrin Van Huff</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90036.0</td>\n",
       "      <td>West</td>\n",
       "      <td>OFF-LA-10000240</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Labels</td>\n",
       "      <td>Self-Adhesive Address Labels for Typewriters b...</td>\n",
       "      <td>14.6200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>US-2016-108966</td>\n",
       "      <td>2016-10-11</td>\n",
       "      <td>2016-10-18</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>SO-20335</td>\n",
       "      <td>Sean O'Donnell</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Fort Lauderdale</td>\n",
       "      <td>Florida</td>\n",
       "      <td>33311.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-TA-10000577</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Tables</td>\n",
       "      <td>Bretford CR4500 Series Slim Rectangular Table</td>\n",
       "      <td>957.5775</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>US-2016-108966</td>\n",
       "      <td>2016-10-11</td>\n",
       "      <td>2016-10-18</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>SO-20335</td>\n",
       "      <td>Sean O'Donnell</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Fort Lauderdale</td>\n",
       "      <td>Florida</td>\n",
       "      <td>33311.0</td>\n",
       "      <td>South</td>\n",
       "      <td>OFF-ST-10000760</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Storage</td>\n",
       "      <td>Eldon Fold 'N Roll Cart System</td>\n",
       "      <td>22.3680</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   row_id        order_id order_date  ship_date       ship_mode customer_id  \\\n",
       "0       1  CA-2017-152156 2017-11-08 2017-11-11    Second Class    CG-12520   \n",
       "1       2  CA-2017-152156 2017-11-08 2017-11-11    Second Class    CG-12520   \n",
       "2       3  CA-2017-138688 2017-06-12 2017-06-16    Second Class    DV-13045   \n",
       "3       4  US-2016-108966 2016-10-11 2016-10-18  Standard Class    SO-20335   \n",
       "4       5  US-2016-108966 2016-10-11 2016-10-18  Standard Class    SO-20335   \n",
       "\n",
       "     customer_name    segment        country             city       state  \\\n",
       "0      Claire Gute   Consumer  United States        Henderson    Kentucky   \n",
       "1      Claire Gute   Consumer  United States        Henderson    Kentucky   \n",
       "2  Darrin Van Huff  Corporate  United States      Los Angeles  California   \n",
       "3   Sean O'Donnell   Consumer  United States  Fort Lauderdale     Florida   \n",
       "4   Sean O'Donnell   Consumer  United States  Fort Lauderdale     Florida   \n",
       "\n",
       "   postal_code region       product_id         category sub_category  \\\n",
       "0      42420.0  South  FUR-BO-10001798        Furniture    Bookcases   \n",
       "1      42420.0  South  FUR-CH-10000454        Furniture       Chairs   \n",
       "2      90036.0   West  OFF-LA-10000240  Office Supplies       Labels   \n",
       "3      33311.0  South  FUR-TA-10000577        Furniture       Tables   \n",
       "4      33311.0  South  OFF-ST-10000760  Office Supplies      Storage   \n",
       "\n",
       "                                        product_name     sales  \n",
       "0                  Bush Somerset Collection Bookcase  261.9600  \n",
       "1  Hon Deluxe Fabric Upholstered Stacking Chairs,...  731.9400  \n",
       "2  Self-Adhesive Address Labels for Typewriters b...   14.6200  \n",
       "3      Bretford CR4500 Series Slim Rectangular Table  957.5775  \n",
       "4                     Eldon Fold 'N Roll Cart System   22.3680  "
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "grouped_df = df.groupby('category')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>postal_code</th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>42420.0</td>\n",
       "      <td>261.9600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>84840.0</td>\n",
       "      <td>993.9000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>90036.0</td>\n",
       "      <td>14.6200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>7</td>\n",
       "      <td>118151.0</td>\n",
       "      <td>1951.4775</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>8</td>\n",
       "      <td>123347.0</td>\n",
       "      <td>36.9880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9795</th>\n",
       "      <td>28842865</td>\n",
       "      <td>324811420.0</td>\n",
       "      <td>705411.9660</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9796</th>\n",
       "      <td>28852662</td>\n",
       "      <td>324855035.0</td>\n",
       "      <td>705422.3340</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9797</th>\n",
       "      <td>8884651</td>\n",
       "      <td>100590645.0</td>\n",
       "      <td>827419.1130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9798</th>\n",
       "      <td>8894450</td>\n",
       "      <td>100634260.0</td>\n",
       "      <td>827445.4890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9799</th>\n",
       "      <td>8904250</td>\n",
       "      <td>100677875.0</td>\n",
       "      <td>827455.8730</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>9800 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        row_id  postal_code        sales\n",
       "0            1      42420.0     261.9600\n",
       "1            3      84840.0     993.9000\n",
       "2            3      90036.0      14.6200\n",
       "3            7     118151.0    1951.4775\n",
       "4            8     123347.0      36.9880\n",
       "...        ...          ...          ...\n",
       "9795  28842865  324811420.0  705411.9660\n",
       "9796  28852662  324855035.0  705422.3340\n",
       "9797   8884651  100590645.0  827419.1130\n",
       "9798   8894450  100634260.0  827445.4890\n",
       "9799   8904250  100677875.0  827455.8730\n",
       "\n",
       "[9800 rows x 3 columns]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df.cumsum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Built-in Transformation\n",
    "\n",
    "| Method | Description |\n",
    "|:-----|:-----|\n",
    "| bfill() | Back fill NA values within each group |\n",
    "| cumcount() | Compute the cumulative count within each group |\n",
    "| cummax() | Compute the cumulative max within each group |\n",
    "| cummin() | Compute the cumulative min within each group |\n",
    "| cumprod() | Compute the cumulative product within each group |\n",
    "| cumsum() | Compute the cumulative sum within each group |\n",
    "| diff() | Compute the difference between adjacent values within each group |\n",
    "| ffill() | Forward fill NA values within each group |\n",
    "| fillna() | Fill NA values within each group |\n",
    "| pct_change() | Compute the percent change between adjacent values within each group |\n",
    "| rank() | Compute the rank of each value within each group |\n",
    "| shift() | Shift values up or down within each group"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Transformation with User-Defined Functions\n",
    "\n",
    "Similar to the aggregation method, the `transform()` method can accept string aliases to the built-in transformation methods in the previous section. It can also accept string aliases to the built-in aggregation methods. When an aggregation method is provided, the result will be broadcast across the group.\n",
    "\n",
    "In addition to string aliases, the transform() method can also accept User-Defined Functions (UDFs). The UDF must:\n",
    "\n",
    "**Note:** \n",
    "Transforming by supplying `transform` with a UDF is often less performant than using the built-in methods on GroupBy. Consider breaking up a complex operation into a chain of operations that utilize the built-in methods."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Solving a Case Study using Groupby"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Reading a .csv File - Online Store Sales Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Row ID</th>\n",
       "      <th>Order ID</th>\n",
       "      <th>Order Date</th>\n",
       "      <th>Ship Date</th>\n",
       "      <th>Ship Mode</th>\n",
       "      <th>Customer ID</th>\n",
       "      <th>Customer Name</th>\n",
       "      <th>Segment</th>\n",
       "      <th>Country</th>\n",
       "      <th>City</th>\n",
       "      <th>State</th>\n",
       "      <th>Postal Code</th>\n",
       "      <th>Region</th>\n",
       "      <th>Product ID</th>\n",
       "      <th>Category</th>\n",
       "      <th>Sub-Category</th>\n",
       "      <th>Product Name</th>\n",
       "      <th>Sales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>CA-2017-152156</td>\n",
       "      <td>2017-11-08</td>\n",
       "      <td>2017-11-11</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>CG-12520</td>\n",
       "      <td>Claire Gute</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Henderson</td>\n",
       "      <td>Kentucky</td>\n",
       "      <td>42420.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-BO-10001798</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Bookcases</td>\n",
       "      <td>Bush Somerset Collection Bookcase</td>\n",
       "      <td>261.9600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>CA-2017-152156</td>\n",
       "      <td>2017-11-08</td>\n",
       "      <td>2017-11-11</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>CG-12520</td>\n",
       "      <td>Claire Gute</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Henderson</td>\n",
       "      <td>Kentucky</td>\n",
       "      <td>42420.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-CH-10000454</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Chairs</td>\n",
       "      <td>Hon Deluxe Fabric Upholstered Stacking Chairs,...</td>\n",
       "      <td>731.9400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>CA-2017-138688</td>\n",
       "      <td>2017-06-12</td>\n",
       "      <td>2017-06-16</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>DV-13045</td>\n",
       "      <td>Darrin Van Huff</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90036.0</td>\n",
       "      <td>West</td>\n",
       "      <td>OFF-LA-10000240</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Labels</td>\n",
       "      <td>Self-Adhesive Address Labels for Typewriters b...</td>\n",
       "      <td>14.6200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>US-2016-108966</td>\n",
       "      <td>2016-10-11</td>\n",
       "      <td>2016-10-18</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>SO-20335</td>\n",
       "      <td>Sean O'Donnell</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Fort Lauderdale</td>\n",
       "      <td>Florida</td>\n",
       "      <td>33311.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-TA-10000577</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Tables</td>\n",
       "      <td>Bretford CR4500 Series Slim Rectangular Table</td>\n",
       "      <td>957.5775</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>US-2016-108966</td>\n",
       "      <td>2016-10-11</td>\n",
       "      <td>2016-10-18</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>SO-20335</td>\n",
       "      <td>Sean O'Donnell</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Fort Lauderdale</td>\n",
       "      <td>Florida</td>\n",
       "      <td>33311.0</td>\n",
       "      <td>South</td>\n",
       "      <td>OFF-ST-10000760</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Storage</td>\n",
       "      <td>Eldon Fold 'N Roll Cart System</td>\n",
       "      <td>22.3680</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Row ID        Order ID Order Date  Ship Date       Ship Mode Customer ID  \\\n",
       "0       1  CA-2017-152156 2017-11-08 2017-11-11    Second Class    CG-12520   \n",
       "1       2  CA-2017-152156 2017-11-08 2017-11-11    Second Class    CG-12520   \n",
       "2       3  CA-2017-138688 2017-06-12 2017-06-16    Second Class    DV-13045   \n",
       "3       4  US-2016-108966 2016-10-11 2016-10-18  Standard Class    SO-20335   \n",
       "4       5  US-2016-108966 2016-10-11 2016-10-18  Standard Class    SO-20335   \n",
       "\n",
       "     Customer Name    Segment        Country             City       State  \\\n",
       "0      Claire Gute   Consumer  United States        Henderson    Kentucky   \n",
       "1      Claire Gute   Consumer  United States        Henderson    Kentucky   \n",
       "2  Darrin Van Huff  Corporate  United States      Los Angeles  California   \n",
       "3   Sean O'Donnell   Consumer  United States  Fort Lauderdale     Florida   \n",
       "4   Sean O'Donnell   Consumer  United States  Fort Lauderdale     Florida   \n",
       "\n",
       "   Postal Code Region       Product ID         Category Sub-Category  \\\n",
       "0      42420.0  South  FUR-BO-10001798        Furniture    Bookcases   \n",
       "1      42420.0  South  FUR-CH-10000454        Furniture       Chairs   \n",
       "2      90036.0   West  OFF-LA-10000240  Office Supplies       Labels   \n",
       "3      33311.0  South  FUR-TA-10000577        Furniture       Tables   \n",
       "4      33311.0  South  OFF-ST-10000760  Office Supplies      Storage   \n",
       "\n",
       "                                        Product Name     Sales  \n",
       "0                  Bush Somerset Collection Bookcase  261.9600  \n",
       "1  Hon Deluxe Fabric Upholstered Stacking Chairs,...  731.9400  \n",
       "2  Self-Adhesive Address Labels for Typewriters b...   14.6200  \n",
       "3      Bretford CR4500 Series Slim Rectangular Table  957.5775  \n",
       "4                     Eldon Fold 'N Roll Cart System   22.3680  "
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('data/online_store_sales.csv', parse_dates=[\"Order Date\", \"Ship Date\"], dayfirst=True)\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**What comes to my mind immediately after looking at the dataset?**\n",
    "\n",
    "> 1. What are the different customer segments?\n",
    "> 2. How many sales records do we have in the dataset?\n",
    "> 3. What are the different product categories?\n",
    "> 4. How many days on average it takes for the products to get shipped?\n",
    "> 5. Are there more orders placed on weekends?\n",
    "> 6. What is the minimum order amount and maximum order amount?\n",
    "> 7. Which customer contributed to the maximum revenue in 2017 and how much?\n",
    "> 8. What is the revenue generated in the year 2017?\n",
    "> 9. Which region recorded maximum sales count?\n",
    "> 10. Which product category is doing best? (revenue and count)\n",
    "\n",
    "**Let's try to answer all the questions.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['row_id', 'order_id', 'order_date', 'ship_date', 'ship_mode',\n",
       "       'customer_id', 'customer_name', 'segment', 'country', 'city', 'state',\n",
       "       'postal_code', 'region', 'product_id', 'category', 'sub_category',\n",
       "       'product_name', 'sales'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "col_names = [ col.strip().lower().replace(' ', '_').replace('-', '_') for col in df.columns ]\n",
    "\n",
    "df.columns = col_names\n",
    "\n",
    "df.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### What are the different customer segments?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Customer Segments:\n",
      " ['Consumer' 'Corporate' 'Home Office']\n"
     ]
    }
   ],
   "source": [
    "print(\"Customer Segments:\\n\", df['segment'].unique())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How many sales records do we have in the dataset?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total Sales Records: 9800\n"
     ]
    }
   ],
   "source": [
    "print(\"Total Sales Records:\", df.shape[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### What are the different product categories?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Product Categories:\n",
      " ['Furniture' 'Office Supplies' 'Technology']\n"
     ]
    }
   ],
   "source": [
    "print(\"Product Categories:\\n\", df['category'].unique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Product Categories:\n",
      " ['Bookcases' 'Chairs' 'Labels' 'Tables' 'Storage' 'Furnishings' 'Art'\n",
      " 'Phones' 'Binders' 'Appliances' 'Paper' 'Accessories' 'Envelopes'\n",
      " 'Fasteners' 'Supplies' 'Machines' 'Copiers']\n"
     ]
    }
   ],
   "source": [
    "print(\"Product Categories:\\n\", df['sub_category'].unique())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How many days on average it take for the products to get shipped?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>order_id</th>\n",
       "      <th>order_date</th>\n",
       "      <th>ship_date</th>\n",
       "      <th>ship_mode</th>\n",
       "      <th>customer_id</th>\n",
       "      <th>customer_name</th>\n",
       "      <th>segment</th>\n",
       "      <th>country</th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "      <th>postal_code</th>\n",
       "      <th>region</th>\n",
       "      <th>product_id</th>\n",
       "      <th>category</th>\n",
       "      <th>sub_category</th>\n",
       "      <th>product_name</th>\n",
       "      <th>sales</th>\n",
       "      <th>ship_time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>CA-2017-152156</td>\n",
       "      <td>2017-11-08</td>\n",
       "      <td>2017-11-11</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>CG-12520</td>\n",
       "      <td>Claire Gute</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Henderson</td>\n",
       "      <td>Kentucky</td>\n",
       "      <td>42420.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-BO-10001798</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Bookcases</td>\n",
       "      <td>Bush Somerset Collection Bookcase</td>\n",
       "      <td>261.9600</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>CA-2017-152156</td>\n",
       "      <td>2017-11-08</td>\n",
       "      <td>2017-11-11</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>CG-12520</td>\n",
       "      <td>Claire Gute</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Henderson</td>\n",
       "      <td>Kentucky</td>\n",
       "      <td>42420.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-CH-10000454</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Chairs</td>\n",
       "      <td>Hon Deluxe Fabric Upholstered Stacking Chairs,...</td>\n",
       "      <td>731.9400</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>CA-2017-138688</td>\n",
       "      <td>2017-06-12</td>\n",
       "      <td>2017-06-16</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>DV-13045</td>\n",
       "      <td>Darrin Van Huff</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90036.0</td>\n",
       "      <td>West</td>\n",
       "      <td>OFF-LA-10000240</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Labels</td>\n",
       "      <td>Self-Adhesive Address Labels for Typewriters b...</td>\n",
       "      <td>14.6200</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>US-2016-108966</td>\n",
       "      <td>2016-10-11</td>\n",
       "      <td>2016-10-18</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>SO-20335</td>\n",
       "      <td>Sean O'Donnell</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Fort Lauderdale</td>\n",
       "      <td>Florida</td>\n",
       "      <td>33311.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-TA-10000577</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Tables</td>\n",
       "      <td>Bretford CR4500 Series Slim Rectangular Table</td>\n",
       "      <td>957.5775</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>US-2016-108966</td>\n",
       "      <td>2016-10-11</td>\n",
       "      <td>2016-10-18</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>SO-20335</td>\n",
       "      <td>Sean O'Donnell</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Fort Lauderdale</td>\n",
       "      <td>Florida</td>\n",
       "      <td>33311.0</td>\n",
       "      <td>South</td>\n",
       "      <td>OFF-ST-10000760</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Storage</td>\n",
       "      <td>Eldon Fold 'N Roll Cart System</td>\n",
       "      <td>22.3680</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   row_id        order_id order_date  ship_date       ship_mode customer_id  \\\n",
       "0       1  CA-2017-152156 2017-11-08 2017-11-11    Second Class    CG-12520   \n",
       "1       2  CA-2017-152156 2017-11-08 2017-11-11    Second Class    CG-12520   \n",
       "2       3  CA-2017-138688 2017-06-12 2017-06-16    Second Class    DV-13045   \n",
       "3       4  US-2016-108966 2016-10-11 2016-10-18  Standard Class    SO-20335   \n",
       "4       5  US-2016-108966 2016-10-11 2016-10-18  Standard Class    SO-20335   \n",
       "\n",
       "     customer_name    segment        country             city       state  \\\n",
       "0      Claire Gute   Consumer  United States        Henderson    Kentucky   \n",
       "1      Claire Gute   Consumer  United States        Henderson    Kentucky   \n",
       "2  Darrin Van Huff  Corporate  United States      Los Angeles  California   \n",
       "3   Sean O'Donnell   Consumer  United States  Fort Lauderdale     Florida   \n",
       "4   Sean O'Donnell   Consumer  United States  Fort Lauderdale     Florida   \n",
       "\n",
       "   postal_code region       product_id         category sub_category  \\\n",
       "0      42420.0  South  FUR-BO-10001798        Furniture    Bookcases   \n",
       "1      42420.0  South  FUR-CH-10000454        Furniture       Chairs   \n",
       "2      90036.0   West  OFF-LA-10000240  Office Supplies       Labels   \n",
       "3      33311.0  South  FUR-TA-10000577        Furniture       Tables   \n",
       "4      33311.0  South  OFF-ST-10000760  Office Supplies      Storage   \n",
       "\n",
       "                                        product_name     sales  ship_time  \n",
       "0                  Bush Somerset Collection Bookcase  261.9600          3  \n",
       "1  Hon Deluxe Fabric Upholstered Stacking Chairs,...  731.9400          3  \n",
       "2  Self-Adhesive Address Labels for Typewriters b...   14.6200          4  \n",
       "3      Bretford CR4500 Series Slim Rectangular Table  957.5775          7  \n",
       "4                     Eldon Fold 'N Roll Cart System   22.3680          7  "
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['ship_time'] = df['ship_date'] - df['order_date']\n",
    "\n",
    "df['ship_time'] = df['ship_time'].dt.days\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Average ship time is 3.9611224489795918 days.\n"
     ]
    }
   ],
   "source": [
    "print(\"Average ship time is\", df['ship_time'].mean(), 'days.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Are there more orders placed on weekends?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       Wednesday\n",
       "1       Wednesday\n",
       "2          Monday\n",
       "3         Tuesday\n",
       "4         Tuesday\n",
       "          ...    \n",
       "9795       Sunday\n",
       "9796      Tuesday\n",
       "9797      Tuesday\n",
       "9798      Tuesday\n",
       "9799      Tuesday\n",
       "Name: order_date, Length: 9800, dtype: object"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['order_date'].dt.day_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tuesday      1889\n",
       "Saturday     1786\n",
       "Sunday       1695\n",
       "Monday       1593\n",
       "Wednesday    1229\n",
       "Friday       1067\n",
       "Thursday      541\n",
       "Name: week_day, dtype: int64"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['week_day'] = df['order_date'].dt.day_name()\n",
    "\n",
    "df.week_day.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "week_day\n",
       "Tuesday      1889\n",
       "Saturday     1786\n",
       "Sunday       1695\n",
       "Monday       1593\n",
       "Wednesday    1229\n",
       "Friday       1067\n",
       "Thursday      541\n",
       "Name: week_day, dtype: int64"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df = df.groupby('week_day')\n",
    "\n",
    "grouped_df['week_day'].count().sort_values(ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "week_day\n",
       "Saturday     420901.4763\n",
       "Tuesday      420535.9243\n",
       "Sunday       377868.7779\n",
       "Monday       348791.5516\n",
       "Wednesday    315888.9722\n",
       "Friday       234710.8402\n",
       "Thursday     142839.2402\n",
       "Name: sales, dtype: float64"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Not just this we can also know the maximum revenue generated on which week day?\n",
    "\n",
    "grouped_df['sales'].sum().sort_values(ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### What is the minimum order amount and maximum order amount?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>order_id</th>\n",
       "      <th>order_date</th>\n",
       "      <th>ship_date</th>\n",
       "      <th>ship_mode</th>\n",
       "      <th>customer_id</th>\n",
       "      <th>customer_name</th>\n",
       "      <th>segment</th>\n",
       "      <th>country</th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "      <th>postal_code</th>\n",
       "      <th>region</th>\n",
       "      <th>product_id</th>\n",
       "      <th>category</th>\n",
       "      <th>sub_category</th>\n",
       "      <th>product_name</th>\n",
       "      <th>sales</th>\n",
       "      <th>ship_time</th>\n",
       "      <th>week_day</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>CA-2017-152156</td>\n",
       "      <td>2017-11-08</td>\n",
       "      <td>2017-11-11</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>CG-12520</td>\n",
       "      <td>Claire Gute</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Henderson</td>\n",
       "      <td>Kentucky</td>\n",
       "      <td>42420.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-BO-10001798</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Bookcases</td>\n",
       "      <td>Bush Somerset Collection Bookcase</td>\n",
       "      <td>261.9600</td>\n",
       "      <td>3</td>\n",
       "      <td>Wednesday</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>CA-2017-152156</td>\n",
       "      <td>2017-11-08</td>\n",
       "      <td>2017-11-11</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>CG-12520</td>\n",
       "      <td>Claire Gute</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Henderson</td>\n",
       "      <td>Kentucky</td>\n",
       "      <td>42420.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-CH-10000454</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Chairs</td>\n",
       "      <td>Hon Deluxe Fabric Upholstered Stacking Chairs,...</td>\n",
       "      <td>731.9400</td>\n",
       "      <td>3</td>\n",
       "      <td>Wednesday</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>CA-2017-138688</td>\n",
       "      <td>2017-06-12</td>\n",
       "      <td>2017-06-16</td>\n",
       "      <td>Second Class</td>\n",
       "      <td>DV-13045</td>\n",
       "      <td>Darrin Van Huff</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>United States</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>California</td>\n",
       "      <td>90036.0</td>\n",
       "      <td>West</td>\n",
       "      <td>OFF-LA-10000240</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Labels</td>\n",
       "      <td>Self-Adhesive Address Labels for Typewriters b...</td>\n",
       "      <td>14.6200</td>\n",
       "      <td>4</td>\n",
       "      <td>Monday</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>US-2016-108966</td>\n",
       "      <td>2016-10-11</td>\n",
       "      <td>2016-10-18</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>SO-20335</td>\n",
       "      <td>Sean O'Donnell</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Fort Lauderdale</td>\n",
       "      <td>Florida</td>\n",
       "      <td>33311.0</td>\n",
       "      <td>South</td>\n",
       "      <td>FUR-TA-10000577</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Tables</td>\n",
       "      <td>Bretford CR4500 Series Slim Rectangular Table</td>\n",
       "      <td>957.5775</td>\n",
       "      <td>7</td>\n",
       "      <td>Tuesday</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>US-2016-108966</td>\n",
       "      <td>2016-10-11</td>\n",
       "      <td>2016-10-18</td>\n",
       "      <td>Standard Class</td>\n",
       "      <td>SO-20335</td>\n",
       "      <td>Sean O'Donnell</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>United States</td>\n",
       "      <td>Fort Lauderdale</td>\n",
       "      <td>Florida</td>\n",
       "      <td>33311.0</td>\n",
       "      <td>South</td>\n",
       "      <td>OFF-ST-10000760</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Storage</td>\n",
       "      <td>Eldon Fold 'N Roll Cart System</td>\n",
       "      <td>22.3680</td>\n",
       "      <td>7</td>\n",
       "      <td>Tuesday</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   row_id        order_id order_date  ship_date       ship_mode customer_id  \\\n",
       "0       1  CA-2017-152156 2017-11-08 2017-11-11    Second Class    CG-12520   \n",
       "1       2  CA-2017-152156 2017-11-08 2017-11-11    Second Class    CG-12520   \n",
       "2       3  CA-2017-138688 2017-06-12 2017-06-16    Second Class    DV-13045   \n",
       "3       4  US-2016-108966 2016-10-11 2016-10-18  Standard Class    SO-20335   \n",
       "4       5  US-2016-108966 2016-10-11 2016-10-18  Standard Class    SO-20335   \n",
       "\n",
       "     customer_name    segment        country             city       state  \\\n",
       "0      Claire Gute   Consumer  United States        Henderson    Kentucky   \n",
       "1      Claire Gute   Consumer  United States        Henderson    Kentucky   \n",
       "2  Darrin Van Huff  Corporate  United States      Los Angeles  California   \n",
       "3   Sean O'Donnell   Consumer  United States  Fort Lauderdale     Florida   \n",
       "4   Sean O'Donnell   Consumer  United States  Fort Lauderdale     Florida   \n",
       "\n",
       "   postal_code region       product_id         category sub_category  \\\n",
       "0      42420.0  South  FUR-BO-10001798        Furniture    Bookcases   \n",
       "1      42420.0  South  FUR-CH-10000454        Furniture       Chairs   \n",
       "2      90036.0   West  OFF-LA-10000240  Office Supplies       Labels   \n",
       "3      33311.0  South  FUR-TA-10000577        Furniture       Tables   \n",
       "4      33311.0  South  OFF-ST-10000760  Office Supplies      Storage   \n",
       "\n",
       "                                        product_name     sales  ship_time  \\\n",
       "0                  Bush Somerset Collection Bookcase  261.9600          3   \n",
       "1  Hon Deluxe Fabric Upholstered Stacking Chairs,...  731.9400          3   \n",
       "2  Self-Adhesive Address Labels for Typewriters b...   14.6200          4   \n",
       "3      Bretford CR4500 Series Slim Rectangular Table  957.5775          7   \n",
       "4                     Eldon Fold 'N Roll Cart System   22.3680          7   \n",
       "\n",
       "    week_day  \n",
       "0  Wednesday  \n",
       "1  Wednesday  \n",
       "2     Monday  \n",
       "3    Tuesday  \n",
       "4    Tuesday  "
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "grouped_df = df.groupby('order_id')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Minimum Order Amount: order_id\n",
      "CA-2015-100006    377.970\n",
      "CA-2015-100090    196.704\n",
      "CA-2015-100293     91.056\n",
      "CA-2015-100328      3.928\n",
      "CA-2015-100363      2.368\n",
      "                   ...   \n",
      "US-2018-168802     18.368\n",
      "US-2018-169320     11.680\n",
      "US-2018-169488     16.900\n",
      "US-2018-169502     21.810\n",
      "US-2018-169551     13.392\n",
      "Name: sales, Length: 4922, dtype: float64\n",
      "Maximum Order Amount: order_id\n",
      "CA-2015-100006    377.970\n",
      "CA-2015-100090    502.488\n",
      "CA-2015-100293     91.056\n",
      "CA-2015-100328      3.928\n",
      "CA-2015-100363     19.008\n",
      "                   ...   \n",
      "US-2018-168802     18.368\n",
      "US-2018-169320    159.750\n",
      "US-2018-169488     39.960\n",
      "US-2018-169502     91.600\n",
      "US-2018-169551    683.988\n",
      "Name: sales, Length: 4922, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print('Minimum Order Amount:', grouped_df['sales'].min())\n",
    "print('Maximum Order Amount:', grouped_df['sales'].max())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**What just happened? 🤯**\n",
    "\n",
    "**This is not what I expected. 😥**\n",
    "\n",
    "**Always remember the basics - Groupby Splits, Aggregation is applied on each group and results are combined and displayed.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "order_id\n",
       "CA-2015-100006     377.970\n",
       "CA-2015-100090     699.192\n",
       "CA-2015-100293      91.056\n",
       "CA-2015-100328       3.928\n",
       "CA-2015-100363      21.376\n",
       "                    ...   \n",
       "US-2018-168802      18.368\n",
       "US-2018-169320     171.430\n",
       "US-2018-169488      56.860\n",
       "US-2018-169502     113.410\n",
       "US-2018-169551    1344.838\n",
       "Name: sales, Length: 4922, dtype: float64"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df['sales'].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>order_id</th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CA-2015-100006</td>\n",
       "      <td>377.970</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>CA-2015-100090</td>\n",
       "      <td>699.192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>CA-2015-100293</td>\n",
       "      <td>91.056</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CA-2015-100328</td>\n",
       "      <td>3.928</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>CA-2015-100363</td>\n",
       "      <td>21.376</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         order_id    sales\n",
       "0  CA-2015-100006  377.970\n",
       "1  CA-2015-100090  699.192\n",
       "2  CA-2015-100293   91.056\n",
       "3  CA-2015-100328    3.928\n",
       "4  CA-2015-100363   21.376"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "order_df = grouped_df['sales'].sum()\n",
    "\n",
    "order_df = order_df.reset_index()\n",
    "\n",
    "order_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Minimum Order Amount: 0.556\n",
      "Maximum Order Amount: 23661.228\n"
     ]
    }
   ],
   "source": [
    "print('Minimum Order Amount:', order_df['sales'].min())\n",
    "print('Maximum Order Amount:', order_df['sales'].max())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### What is the revenue generated in the year 2017?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['order_year'] = df['order_date'].dt.year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "600192.55"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Method 1 - Using filtering and aggregation\n",
    "df.loc[ df['order_year'] == 2017, 'sales' ].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "order_year\n",
       "2015    479856.2081\n",
       "2016    459436.0054\n",
       "2017    600192.5500\n",
       "2018    722052.0192\n",
       "Name: sales, dtype: float64"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Method 2 - Using splitting and aggregation\n",
    "grouped_df = df.groupby(['order_year'])\n",
    "\n",
    "grouped_df['sales'].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "600192.55"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yearwise_revenue_df = grouped_df['sales'].sum()\n",
    "\n",
    "yearwise_revenue_df.loc[2017]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Which customer contributed to the maximum revenue in 2017 and how much?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "grouped_df = df.groupby(['order_year', 'customer_id'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "order_year  customer_id\n",
       "2015        AA-10315        756.048\n",
       "            AA-10375         50.792\n",
       "            AA-10480         27.460\n",
       "            AA-10645       1434.330\n",
       "            AB-10015        322.216\n",
       "                             ...   \n",
       "2018        XP-21865        449.312\n",
       "            YC-21895        750.680\n",
       "            YS-21880       5340.264\n",
       "            ZC-21910        227.066\n",
       "            ZD-21925         61.440\n",
       "Name: sales, Length: 2481, dtype: float64"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df['sales'].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "order_year  customer_id\n",
       "2015        AA-10315        756.048\n",
       "            AA-10375         50.792\n",
       "            AA-10480         27.460\n",
       "            AA-10645       1434.330\n",
       "            AB-10015        322.216\n",
       "                             ...   \n",
       "2018        XP-21865        449.312\n",
       "            YC-21895        750.680\n",
       "            YS-21880       5340.264\n",
       "            ZC-21910        227.066\n",
       "            ZD-21925         61.440\n",
       "Name: sales, Length: 2481, dtype: float64"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yearwise_cust_revenue_contribution = grouped_df['sales'].sum()\n",
    "\n",
    "yearwise_cust_revenue_contribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18344.052000000003"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yearwise_cust_revenue_contribution.loc[2017].max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'TC-20980'"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yearwise_cust_revenue_contribution.loc[2017].idxmax()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Customer ID: TC-20980 , contributed to the maximum revenue of 18344.052000000003 in 2017\n"
     ]
    }
   ],
   "source": [
    "cust_id = yearwise_cust_revenue_contribution.loc[2017].idxmax()\n",
    "revenue_contributed = yearwise_cust_revenue_contribution.loc[(2017, cust_id)]\n",
    "\n",
    "print(\"Customer ID:\", cust_id, \", contributed to the maximum revenue of\", revenue_contributed, \"in 2017\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total company revenue in 2017:\n",
      "600192.55\n"
     ]
    }
   ],
   "source": [
    "print(\"Total company revenue in 2017:\")\n",
    "print(yearwise_cust_revenue_contribution.loc[2017].sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "customer_id\n",
       "SJ-20215        1.964\n",
       "SH-20395        2.214\n",
       "JW-15955        2.610\n",
       "BM-11650        2.907\n",
       "RW-19690        3.282\n",
       "              ...    \n",
       "BS-11365     9199.780\n",
       "SE-20110     9879.220\n",
       "AB-10105    10403.865\n",
       "CC-12370    11901.184\n",
       "TC-20980    18344.052\n",
       "Name: sales, Length: 635, dtype: float64"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yearwise_cust_revenue_contribution.loc[2017].sort_values()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Who is the customer with `customer_id == TC-20980` ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>order_date</th>\n",
       "      <th>customer_name</th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "      <th>postal_code</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2072</th>\n",
       "      <td>2017-11-26</td>\n",
       "      <td>Tamara Chand</td>\n",
       "      <td>Seattle</td>\n",
       "      <td>Washington</td>\n",
       "      <td>98105.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3185</th>\n",
       "      <td>2015-11-07</td>\n",
       "      <td>Tamara Chand</td>\n",
       "      <td>Houston</td>\n",
       "      <td>Texas</td>\n",
       "      <td>77041.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3186</th>\n",
       "      <td>2015-11-07</td>\n",
       "      <td>Tamara Chand</td>\n",
       "      <td>Houston</td>\n",
       "      <td>Texas</td>\n",
       "      <td>77041.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6825</th>\n",
       "      <td>2017-10-02</td>\n",
       "      <td>Tamara Chand</td>\n",
       "      <td>Lafayette</td>\n",
       "      <td>Indiana</td>\n",
       "      <td>47905.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6826</th>\n",
       "      <td>2017-10-02</td>\n",
       "      <td>Tamara Chand</td>\n",
       "      <td>Lafayette</td>\n",
       "      <td>Indiana</td>\n",
       "      <td>47905.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6827</th>\n",
       "      <td>2017-10-02</td>\n",
       "      <td>Tamara Chand</td>\n",
       "      <td>Lafayette</td>\n",
       "      <td>Indiana</td>\n",
       "      <td>47905.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6828</th>\n",
       "      <td>2017-10-02</td>\n",
       "      <td>Tamara Chand</td>\n",
       "      <td>Lafayette</td>\n",
       "      <td>Indiana</td>\n",
       "      <td>47905.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6829</th>\n",
       "      <td>2017-10-02</td>\n",
       "      <td>Tamara Chand</td>\n",
       "      <td>Lafayette</td>\n",
       "      <td>Indiana</td>\n",
       "      <td>47905.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8060</th>\n",
       "      <td>2016-09-20</td>\n",
       "      <td>Tamara Chand</td>\n",
       "      <td>Long Beach</td>\n",
       "      <td>New York</td>\n",
       "      <td>11561.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8061</th>\n",
       "      <td>2016-09-20</td>\n",
       "      <td>Tamara Chand</td>\n",
       "      <td>Long Beach</td>\n",
       "      <td>New York</td>\n",
       "      <td>11561.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8337</th>\n",
       "      <td>2015-12-27</td>\n",
       "      <td>Tamara Chand</td>\n",
       "      <td>Decatur</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>35601.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8338</th>\n",
       "      <td>2015-12-27</td>\n",
       "      <td>Tamara Chand</td>\n",
       "      <td>Decatur</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>35601.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     order_date customer_name        city       state  postal_code\n",
       "2072 2017-11-26  Tamara Chand     Seattle  Washington      98105.0\n",
       "3185 2015-11-07  Tamara Chand     Houston       Texas      77041.0\n",
       "3186 2015-11-07  Tamara Chand     Houston       Texas      77041.0\n",
       "6825 2017-10-02  Tamara Chand   Lafayette     Indiana      47905.0\n",
       "6826 2017-10-02  Tamara Chand   Lafayette     Indiana      47905.0\n",
       "6827 2017-10-02  Tamara Chand   Lafayette     Indiana      47905.0\n",
       "6828 2017-10-02  Tamara Chand   Lafayette     Indiana      47905.0\n",
       "6829 2017-10-02  Tamara Chand   Lafayette     Indiana      47905.0\n",
       "8060 2016-09-20  Tamara Chand  Long Beach    New York      11561.0\n",
       "8061 2016-09-20  Tamara Chand  Long Beach    New York      11561.0\n",
       "8337 2015-12-27  Tamara Chand     Decatur     Alabama      35601.0\n",
       "8338 2015-12-27  Tamara Chand     Decatur     Alabama      35601.0"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[(df.customer_id == 'TC-20980') , ['order_date', 'customer_name', 'city', 'state', 'postal_code']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Which region recorded maximum sales count?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['row_id', 'order_id', 'order_date', 'ship_date', 'ship_mode',\n",
       "       'customer_id', 'customer_name', 'segment', 'country', 'city', 'state',\n",
       "       'postal_code', 'region', 'product_id', 'category', 'sub_category',\n",
       "       'product_name', 'sales', 'ship_time', 'week_day', 'order_year'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "West       3140\n",
       "East       2785\n",
       "Central    2277\n",
       "South      1598\n",
       "Name: region, dtype: int64"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Method 1 - Using value_counts()\n",
    "df.region.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "region\n",
       "Central    2277\n",
       "East       2785\n",
       "South      1598\n",
       "West       3140\n",
       "Name: sales, dtype: int64"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Method 2 - Using split and aggregation\n",
    "\n",
    "grouped_df = df.groupby(\"region\")\n",
    "\n",
    "grouped_df['sales'].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "region\n",
       "Central    492646.9132\n",
       "East       669518.7260\n",
       "South      389151.4590\n",
       "West       710219.6845\n",
       "Name: sales, dtype: float64"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# What if the question is: Which region recorded maximum sales revenue?\n",
    "\n",
    "grouped_df['sales'].sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Which product category is doing best? (revenue and count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "category\n",
       "Furniture          2078\n",
       "Office Supplies    5909\n",
       "Technology         1813\n",
       "Name: sales, dtype: int64"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df = df.groupby('category')\n",
    "\n",
    "grouped_df['sales'].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "category\n",
       "Furniture          728658.5757\n",
       "Office Supplies    705422.3340\n",
       "Technology         827455.8730\n",
       "Name: sales, dtype: float64"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_df['sales'].sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Analysing and Summarizing using pivot_table()\n",
    "\n",
    "**NOTE: Use MS Excel to understand the results of pivot_table()**  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### What is the region-wise revenue?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>region</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Central</th>\n",
       "      <td>492646.9132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>East</th>\n",
       "      <td>669518.7260</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>South</th>\n",
       "      <td>389151.4590</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>West</th>\n",
       "      <td>710219.6845</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               sales\n",
       "region              \n",
       "Central  492646.9132\n",
       "East     669518.7260\n",
       "South    389151.4590\n",
       "West     710219.6845"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(values=\"sales\", \n",
    "               index=[\"region\"], \n",
    "               aggfunc=\"sum\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>region</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Central</th>\n",
       "      <td>492646.91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>East</th>\n",
       "      <td>669518.73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>South</th>\n",
       "      <td>389151.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>West</th>\n",
       "      <td>710219.68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>All</th>\n",
       "      <td>2261536.78</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              sales\n",
       "region             \n",
       "Central   492646.91\n",
       "East      669518.73\n",
       "South     389151.46\n",
       "West      710219.68\n",
       "All      2261536.78"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(values=\"sales\", \n",
    "               index=[\"region\"],\n",
    "               margins=True, \n",
    "               aggfunc=\"sum\").round(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>region</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Central</th>\n",
       "      <td>21.783723</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>East</th>\n",
       "      <td>29.604591</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>South</th>\n",
       "      <td>17.207390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>West</th>\n",
       "      <td>31.404295</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             sales\n",
       "region            \n",
       "Central  21.783723\n",
       "East     29.604591\n",
       "South    17.207390\n",
       "West     31.404295"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(values=\"sales\", \n",
    "               index=[\"region\"], \n",
    "               aggfunc=\"sum\").apply(lambda values: values*100/sum(values))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>region</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Central</th>\n",
       "      <td>21.783723</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>East</th>\n",
       "      <td>29.604591</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>South</th>\n",
       "      <td>17.207390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>West</th>\n",
       "      <td>31.404295</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             sales\n",
       "region            \n",
       "Central  21.783723\n",
       "East     29.604591\n",
       "South    17.207390\n",
       "West     31.404295"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(values=\"sales\", \n",
    "               index=[\"region\"],\n",
    "               aggfunc=\"sum\").apply(lambda values: values*100/sum(values))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### What is the region-wise count of sales?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>region</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Central</th>\n",
       "      <td>2277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>East</th>\n",
       "      <td>2785</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>South</th>\n",
       "      <td>1598</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>West</th>\n",
       "      <td>3140</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         sales\n",
       "region        \n",
       "Central   2277\n",
       "East      2785\n",
       "South     1598\n",
       "West      3140"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(values=\"sales\", \n",
    "               index=[\"region\"], \n",
    "               aggfunc=\"count\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>region</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Central</th>\n",
       "      <td>23.234694</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>East</th>\n",
       "      <td>28.418367</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>South</th>\n",
       "      <td>16.306122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>West</th>\n",
       "      <td>32.040816</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             sales\n",
       "region            \n",
       "Central  23.234694\n",
       "East     28.418367\n",
       "South    16.306122\n",
       "West     32.040816"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(values=\"sales\", \n",
    "               index=[\"region\"], \n",
    "               aggfunc=\"count\").apply(lambda values: values*100/sum(values))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### What is the region-wise count and sum of sales?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>sum</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>sales</th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>region</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Central</th>\n",
       "      <td>2277</td>\n",
       "      <td>492646.9132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>East</th>\n",
       "      <td>2785</td>\n",
       "      <td>669518.7260</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>South</th>\n",
       "      <td>1598</td>\n",
       "      <td>389151.4590</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>West</th>\n",
       "      <td>3140</td>\n",
       "      <td>710219.6845</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        count          sum\n",
       "        sales        sales\n",
       "region                    \n",
       "Central  2277  492646.9132\n",
       "East     2785  669518.7260\n",
       "South    1598  389151.4590\n",
       "West     3140  710219.6845"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(values=\"sales\", \n",
    "               index=[\"region\"], \n",
    "               aggfunc=[\"count\", \"sum\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>sum</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>sales</th>\n",
       "      <th>sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>region</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Central</th>\n",
       "      <td>23.234694</td>\n",
       "      <td>21.783723</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>East</th>\n",
       "      <td>28.418367</td>\n",
       "      <td>29.604591</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>South</th>\n",
       "      <td>16.306122</td>\n",
       "      <td>17.207390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>West</th>\n",
       "      <td>32.040816</td>\n",
       "      <td>31.404295</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             count        sum\n",
       "             sales      sales\n",
       "region                       \n",
       "Central  23.234694  21.783723\n",
       "East     28.418367  29.604591\n",
       "South    16.306122  17.207390\n",
       "West     32.040816  31.404295"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(values=\"sales\", \n",
    "               index=[\"region\"], \n",
    "               aggfunc=[\"count\", \"sum\"]).apply(lambda values: values*100/sum(values))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### What is the region-wise revenue generated of each product category?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>category</th>\n",
       "      <th>Furniture</th>\n",
       "      <th>Office Supplies</th>\n",
       "      <th>Technology</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>region</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Central</th>\n",
       "      <td>160317.4622</td>\n",
       "      <td>163590.243</td>\n",
       "      <td>168739.208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>East</th>\n",
       "      <td>206461.3880</td>\n",
       "      <td>199940.811</td>\n",
       "      <td>263116.527</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>South</th>\n",
       "      <td>116531.4800</td>\n",
       "      <td>124424.771</td>\n",
       "      <td>148195.208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>West</th>\n",
       "      <td>245348.2455</td>\n",
       "      <td>217466.509</td>\n",
       "      <td>247404.930</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "category    Furniture  Office Supplies  Technology\n",
       "region                                            \n",
       "Central   160317.4622       163590.243  168739.208\n",
       "East      206461.3880       199940.811  263116.527\n",
       "South     116531.4800       124424.771  148195.208\n",
       "West      245348.2455       217466.509  247404.930"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(values=\"sales\", \n",
    "               index=[\"region\"], \n",
    "               columns=[\"category\"], \n",
    "               aggfunc=\"sum\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>category</th>\n",
       "      <th>Furniture</th>\n",
       "      <th>Office Supplies</th>\n",
       "      <th>Technology</th>\n",
       "      <th>All</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>region</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Central</th>\n",
       "      <td>160317.462</td>\n",
       "      <td>163590.243</td>\n",
       "      <td>168739.208</td>\n",
       "      <td>492646.913</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>East</th>\n",
       "      <td>206461.388</td>\n",
       "      <td>199940.811</td>\n",
       "      <td>263116.527</td>\n",
       "      <td>669518.726</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>South</th>\n",
       "      <td>116531.480</td>\n",
       "      <td>124424.771</td>\n",
       "      <td>148195.208</td>\n",
       "      <td>389151.459</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>West</th>\n",
       "      <td>245348.246</td>\n",
       "      <td>217466.509</td>\n",
       "      <td>247404.930</td>\n",
       "      <td>710219.684</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>All</th>\n",
       "      <td>728658.576</td>\n",
       "      <td>705422.334</td>\n",
       "      <td>827455.873</td>\n",
       "      <td>2261536.783</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "category   Furniture  Office Supplies  Technology          All\n",
       "region                                                        \n",
       "Central   160317.462       163590.243  168739.208   492646.913\n",
       "East      206461.388       199940.811  263116.527   669518.726\n",
       "South     116531.480       124424.771  148195.208   389151.459\n",
       "West      245348.246       217466.509  247404.930   710219.684\n",
       "All       728658.576       705422.334  827455.873  2261536.783"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(values=\"sales\", \n",
    "               index=[\"region\"], \n",
    "               columns=[\"category\"], \n",
    "               margins=True, \n",
    "               aggfunc=\"sum\").round(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>region</th>\n",
       "      <th>Central</th>\n",
       "      <th>East</th>\n",
       "      <th>South</th>\n",
       "      <th>West</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>category</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Furniture</th>\n",
       "      <td>160317.4622</td>\n",
       "      <td>206461.388</td>\n",
       "      <td>116531.480</td>\n",
       "      <td>245348.2455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Office Supplies</th>\n",
       "      <td>163590.2430</td>\n",
       "      <td>199940.811</td>\n",
       "      <td>124424.771</td>\n",
       "      <td>217466.5090</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Technology</th>\n",
       "      <td>168739.2080</td>\n",
       "      <td>263116.527</td>\n",
       "      <td>148195.208</td>\n",
       "      <td>247404.9300</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "region               Central        East       South         West\n",
       "category                                                         \n",
       "Furniture        160317.4622  206461.388  116531.480  245348.2455\n",
       "Office Supplies  163590.2430  199940.811  124424.771  217466.5090\n",
       "Technology       168739.2080  263116.527  148195.208  247404.9300"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(values=\"sales\", \n",
    "               index=[\"category\"], \n",
    "               columns=[\"region\"], \n",
    "               aggfunc=\"sum\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>region</th>\n",
       "      <th>Central</th>\n",
       "      <th>East</th>\n",
       "      <th>South</th>\n",
       "      <th>West</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>category</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Furniture</th>\n",
       "      <td>470</td>\n",
       "      <td>591</td>\n",
       "      <td>326</td>\n",
       "      <td>691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Office Supplies</th>\n",
       "      <td>1399</td>\n",
       "      <td>1667</td>\n",
       "      <td>983</td>\n",
       "      <td>1860</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Technology</th>\n",
       "      <td>408</td>\n",
       "      <td>527</td>\n",
       "      <td>289</td>\n",
       "      <td>589</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "region           Central  East  South  West\n",
       "category                                   \n",
       "Furniture            470   591    326   691\n",
       "Office Supplies     1399  1667    983  1860\n",
       "Technology           408   527    289   589"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(values=\"sales\", \n",
    "               index=[\"category\"], \n",
    "               columns=[\"region\"], \n",
    "               aggfunc=\"count\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### What is the region-wise revenue generated of each product sub-category under product category?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>region</th>\n",
       "      <th>Central</th>\n",
       "      <th>East</th>\n",
       "      <th>South</th>\n",
       "      <th>West</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>category</th>\n",
       "      <th>sub_category</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Furniture</th>\n",
       "      <th>Bookcases</th>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "      <td>28</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chairs</th>\n",
       "      <td>151</td>\n",
       "      <td>167</td>\n",
       "      <td>86</td>\n",
       "      <td>203</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Furnishings</th>\n",
       "      <td>198</td>\n",
       "      <td>275</td>\n",
       "      <td>162</td>\n",
       "      <td>296</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tables</th>\n",
       "      <td>72</td>\n",
       "      <td>79</td>\n",
       "      <td>50</td>\n",
       "      <td>113</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"9\" valign=\"top\">Office Supplies</th>\n",
       "      <th>Appliances</th>\n",
       "      <td>122</td>\n",
       "      <td>123</td>\n",
       "      <td>81</td>\n",
       "      <td>133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Art</th>\n",
       "      <td>175</td>\n",
       "      <td>225</td>\n",
       "      <td>140</td>\n",
       "      <td>245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Binders</th>\n",
       "      <td>362</td>\n",
       "      <td>427</td>\n",
       "      <td>241</td>\n",
       "      <td>462</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Envelopes</th>\n",
       "      <td>58</td>\n",
       "      <td>70</td>\n",
       "      <td>54</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Fasteners</th>\n",
       "      <td>53</td>\n",
       "      <td>61</td>\n",
       "      <td>29</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Labels</th>\n",
       "      <td>75</td>\n",
       "      <td>105</td>\n",
       "      <td>64</td>\n",
       "      <td>113</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Paper</th>\n",
       "      <td>313</td>\n",
       "      <td>367</td>\n",
       "      <td>218</td>\n",
       "      <td>440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Storage</th>\n",
       "      <td>205</td>\n",
       "      <td>237</td>\n",
       "      <td>127</td>\n",
       "      <td>263</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Supplies</th>\n",
       "      <td>36</td>\n",
       "      <td>52</td>\n",
       "      <td>29</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Technology</th>\n",
       "      <th>Accessories</th>\n",
       "      <td>174</td>\n",
       "      <td>203</td>\n",
       "      <td>125</td>\n",
       "      <td>254</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Copiers</th>\n",
       "      <td>16</td>\n",
       "      <td>20</td>\n",
       "      <td>7</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Machines</th>\n",
       "      <td>21</td>\n",
       "      <td>37</td>\n",
       "      <td>18</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Phones</th>\n",
       "      <td>197</td>\n",
       "      <td>267</td>\n",
       "      <td>139</td>\n",
       "      <td>273</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "region                        Central  East  South  West\n",
       "category        sub_category                            \n",
       "Furniture       Bookcases          49    70     28    79\n",
       "                Chairs            151   167     86   203\n",
       "                Furnishings       198   275    162   296\n",
       "                Tables             72    79     50   113\n",
       "Office Supplies Appliances        122   123     81   133\n",
       "                Art               175   225    140   245\n",
       "                Binders           362   427    241   462\n",
       "                Envelopes          58    70     54    66\n",
       "                Fasteners          53    61     29    71\n",
       "                Labels             75   105     64   113\n",
       "                Paper             313   367    218   440\n",
       "                Storage           205   237    127   263\n",
       "                Supplies           36    52     29    67\n",
       "Technology      Accessories       174   203    125   254\n",
       "                Copiers            16    20      7    23\n",
       "                Machines           21    37     18    39\n",
       "                Phones            197   267    139   273"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(values=\"sales\", \n",
    "               index=[\"category\", 'sub_category'], \n",
    "               columns=[\"region\"], \n",
    "               aggfunc=\"count\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
